问题
I would like to stop any discount code being applied when purchasing a certain product (in this case a Gift Card). Other items in the cart should be unaffected and have the discount code applied to them.
I know that this can be done on the individual coupon creation page, but with hundreds of coupons already made I'd like a way for this to apply automatically.
All help is very appreciated!
This the closest code I have found, but it relates to a plugin rather than woocommerce in general.
add_filter('eha_dp_skip_product','skip_product_from_discount',1,4);
function skip_product_from_discount($return_val,$pid,$rule,$mode)
{
$pid_to_skip=array(53189); // specify pids to skip
if($mode=='category_rules' && in_array($pid,$pid_to_skip))
{
return true; // true to skip this product
}
return $return_val;
}
回答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:
function bulk_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_postmeta
table).
2) Set your product ID (or products IDs) in the array (without discount).
3) Paste that code infunction.php
file 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.
回答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');
function yith_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 ( $items as $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' );
}
}
}
来源:https://stackoverflow.com/questions/46714570/disable-coupons-discounts-from-applying-to-defined-woocommerce-products-in-car