Custom Coupon Types in WooCommerce continued

Further to creating a valid new coupon type, you may wish to extend some certain other functions along with it. A good example of this would be validity of the coupon. Sometimes you may wish to add a coupon that can be added at the beginning of the cart cycle but only applies itself once the criteria of the coupon are met.

According to the documentation it is possible by using the correct hook to intercept the is_valid() call of the woocommerce coupon. In reality, this is not always as successful as would be liked. However, this hook whilst it exists, does not override the default behaviour of the built in coupon types, it only allows you to extend the validity to encompass the new coupon type you are creating.

[php]
function force_coupon_to_be_valid( $valid, $coupon ) {
return true;
}
add_filter( ‘woocommerce_coupon_is_valid’, ‘force_coupon_to_be_valid’, 999, 2 );
[/php]

You could easily assume that will override all coupons in the system, however it doesn’t. Woocommerce doesn’t allow you do use that filter to override anything relating to its core systems and unfortunately the default coupon types are defined as core.

The only way to address issues with the core functionality of Woocommerce is to actually adjust the code inside the plugin itself, which of course is not recommended by anyone unless you are certain you will not be upgrading the woocommerce system blindly. Again, not something I would personally recommend. But if you wanted to do it anyway, the validity function can be found in the WC_Coupon class, which in turn can be found in class-wc-coupon.php.

[php]
public function is_valid() { public function is_valid() {
try {
$this->validate_exists();
$this->validate_usage_limit();
$this->validate_user_usage_limit();
$this->validate_expiry_date();
$this->validate_minimum_amount();
$this->validate_maximum_amount();
$this->validate_product_ids();
$this->validate_product_categories();
$this->validate_sale_items();
$this->validate_excluded_items();
$this->validate_cart_excluded_items();
if ( ! apply_filters( ‘woocommerce_coupon_is_valid’, true, $this ) ) {
throw new Exception( self::E_WC_COUPON_INVALID_FILTERED );
}
}
catch ( Exception $e ) {
$this->error_message = $this->get_coupon_error( $e->getMessage() );
return false;
}
return true;
}
[/php]

You can see for yourselves that the code is relatively straight forward and self explanatory and depending on the type of coupon, depends which of those validate functions are called. You can work that bit out for yourselves as it is definitely not recommended to patch these unless you plan on keeping a custom installation of woocommerce installed irrespective of updates issued. Core functionality between v2 and v3 of woocommerce changed dramatically, it is worth baring that in mind when making your decision.

Hopefully that helps understand the hook a little better and where to find the core bits to override for your particular needs.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.