Disable Coupons & Discounts From Applying To Defined Woocommerce Products In Cart
Solution 1:
The classic simple & effective way to do that should be through coupon settings restrictions:
BUT as you have a lot of coupons here is a custom function to bulk update coupons "excluded product" restrictions:
functionbulk_edit_coupon_restrictions(){
// Only for admin users (not accessible) for other users)if( ! current_user_can( 'manage_options' ) ) return;
global$wpdb;
// Set HERE the product IDs without discount$product_ids = array( 37, 67 );
$product_ids = implode( ',', $product_ids ); // String conversion// SQL query: Bulk update coupons "excluded product" restrictions$wpdb->query( "
UPDATE {$wpdb->prefix}postmeta as pm
SET pm.meta_value = '$product_ids'
WHERE pm.meta_key = 'exclude_product_ids'
AND post_id IN (
SELECT p.ID
FROM {$wpdb->prefix}posts as p
WHERE p.post_type = 'shop_coupon'
AND p.post_status = 'publish'
)
" );
}
// Run this function once (and comment it or remove it)
bulk_edit_coupon_restrictions();
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
USAGE
1) Make a database backup (or in particular the
wp_postmetatable). 2) Set your product ID (or products IDs) in the array (without discount). 3) Paste that code infunction.phpfile of your active theme and save it. 4) Under an admin account, browse any page of your web site. 5) Comment or remove that code. 6) You are done and you can check some coupons to see the result.
Solution 2:
I haven't tried the above answer as the Yith Gift Cards Plugin team got back to me. For anyone looking for the answer relating directly to the Yith plugin, here's the code...
if( defined('YITH_YWGC_PREMIUM') ){
if( !function_exists('yith_wcgc_deny_coupon_on_gift_card') ){
add_action('woocommerce_applied_coupon','yith_wcgc_deny_coupon_on_gift_card');
functionyith_wcgc_deny_coupon_on_gift_card($coupon_code){
global$woocommerce;
$the_coupon = new WC_Coupon( $coupon_code );
$excluded_items = $the_coupon->get_excluded_product_ids();
$items = $woocommerce->cart->get_cart();
foreach ( $itemsas$item ):
if( has_term('gift-card','product_type',$item['product_id']) == true ):
$excluded_items[] = $item['product_id'];
endif;
endforeach;
$the_coupon->set_excluded_product_ids($excluded_items);
$the_coupon->save();
wc_add_notice( 'Coupon cannot applied to gift card product', 'error' );
}
}
}

Post a Comment for "Disable Coupons & Discounts From Applying To Defined Woocommerce Products In Cart"