简体   繁体   中英

Is there a hook or plugin to exclude certain product-tags from a woocommerce coupon?

I need to exclude a product tag from a woocommerce coupon.

I've tried to search Google for an applicable plugin, but came up empty.

Ideally, a solution would be a plugin that adds a custom field to the coupon post type. However, I'll settle for hard coding through a hook/filter.

There is a filter you can use to accomplish this woocommerce_coupon_is_valid_for_product .

It accepts 4 parameters, valid, the product, the coupon and values. It's called from class-wc-coupon.php around line 860 in version 3 or so. It should return a boolean ( true/false ).

Note that the code below is incomplete and untested, and is for example purposes only.

add_filter('woocommerce_coupon_is_valid_for_product', 'exclude_product_from_coupon_by_tag', 12, 4);
function exclude_product_from_coupon_by_tag($valid, $product, $coupon, $values ){
    
    //Check if product has tag/s
    $valid = has_term('INSERT_TERM_HERE', 'product_tag', $product);

    return $valid;
}

Hope that helps!

As Seb Toombs said you must use woocommerce_coupon_is_valid_for_product filter to check product validation by tag. If the validation returns you an error you must change the $product with $product->get_id()

add_filter('woocommerce_coupon_is_valid_for_product', 'exclude_product_from_coupon_by_tag', 12, 4);
function exclude_product_from_coupon_by_tag($valid, $product, $coupon, $values ){
    
    //Check if product has tag/s
    $valid = has_term('INSERT_TERM_HERE', 'product_tag', $product->get_id());

    return $valid;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM