Coupons are one of WooCommerce’s greatest assets; everyone loves a discount. However, the default types and restrictions may not be exactly what you’re looking for and you may need to extend the functionality. After being recently commissioned to extend the coupon functionality of a stable (all be it old), 2.3.11 installation, finding the right places to hook into WooCommerce to extend the coupon system proved time consuming.
I thought I could save others the stress of a ‘demanding’ client, with a simple example of how to add a new coupon type in the form of a plugin.
[php]
<?php
if ( ! defined( ‘ABSPATH’ ) ) {
exit; // Exit if accessed directly
}
function webdav_newcoupon_type ($discount_types) {
$discount_types[‘webdav_coupon’] = __(‘WebDav Coupon’, ‘webdav-coupon-plugin’);
return $discount_types;
}
add_filter (‘woocommerce_coupon_discount_types’, ‘webdav_newcoupon_type’, 10, 1);
?>
[/php]
It doesn’t get much more simple than that really, obviously the first bit stops those ‘curious’ people from executing the plugin directly and the rest is fairly self explanatory.
The filter is passed the discount codes in an array, you can manipulate that array however you like, but in this instance we add a new discount type (or coupon type) of webdav_coupon with the description WebDav Coupon and just return the updated array.
It is probably useful to note that it expects the array to be returned, I didn’t try to return a malformed response, so I couldn’t tell you how it breaks if you mess it up. Although in all fairness, if you mess that up you should probably find something else to do.
Hook: woocommerce_coupon_discount_types
So you now have a shiny new Coupon type, lets create an admin panel for your custom controls and fields.
[php]
function webdav_newcoupon_data_tabs( $tabs ) {
// Create the TAB on the options panel of the coupons
$tabs[‘webdav_newcoupon_data’] = array(
‘label’ => __( ‘WebDav Coupon’, ‘webdav-coupon-plugin’ ),
‘target’ => ‘webdav_newcoupon_data’,
‘class’ => ‘webdav_newcoupon_data’
);
return $tabs;
}
add_filter( ‘woocommerce_coupon_data_tabs’, ‘webdav_newcoupon_data_tabs’, 100, 1 );
function webdav_newcoupon_data_panels() {
global $woocommerce, $post;
echo ‘<div id="webdav_newcoupon_data" class="panel woocommerce_options_panel">’;
// Your coupon type options will go in here
echo ‘</div>’;
}
add_action(‘woocommerce_coupon_data_panels’, ‘webdav_newcoupon_data_panels’);
// Save the fields
function webdav_newcoupon_save_data( $post_id ) {
$newcoupon_fields = array (
// Your fields should be listed here as an array of
// strings.
);
foreach ($newcoupon_fields as $field) {
$value = isset($_POST[ $field ]) ? $_POST[ $field ] : ”;
update_post_meta($post_id, $field, $value);
}
}
add_filter( ‘woocommerce_process_shop_coupon_meta’, ‘webdav_newcoupon_save_data’, 10, 1 ); [/php]
Again, not overly complicated, it’s much like creating any other theme or plugin admin page really except we have to create a panel in the coupon admin screen in which we can place our fields or options.
We create a filter for extending the number of Tabs that are on the left column and add our custom tab for our new coupon type to the panel. We then create an action for displaying those fields or options and finally create a filter that intercepts the processing of the meta data and save the contents of our specific fields or options into the coupons meta data.
Never forget, everything is a ‘post’ inside WordPress and is treated as such, even our little old coupons.
Hopefully that basic framework helps save some of you some time, good luck with your own epic coupon enhancements.
Hooks: woocommerce_coupon_data_tabs, woocommerce_coupon_data_panels, woocommerce_process_shop_coupon_meta
Leave a Reply