Specific Category
-
Hi,
I really need help to apply the force coupon to a specific category or product. I have been trying to work with the following code I found in Stack Overflow by adding it to the functions.php in current theme folder, but it brings the whole site down:// Force Coupon codes for Woocommerce
add_action(‘woocommerce_check_cart_items’, ‘mandatory_coupon_code’);
function mandatory_coupon_code()
{
// set Here your categories IDs, slugs or names
$categories = array(18,19,87);
$found = false;// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item )
if( has_term( $categories, ‘product_cat’, $cart_item[‘product_id’] ) ){
// Product category found in cart items
$found = true;
// Exit from loop
break;
}
}
$coupons = WC()->cart->get_applied_coupons();// The Notice is displayed for that product categories when no mandatory coupon has been entered
if( count( $coupons ) > 0 && $found )
wc_add_notice( __( ‘A coupon code is mandatory for this product.’, ‘woocommerce’ ), ‘error’ );I am running the latest version of both WordPress and Woocommerce.
Please could you help me?The page I need help with: [log in to see the link]
- The topic ‘Specific Category’ is closed to new replies.