禁止将优惠券和折扣应用到购物车中已定义的Woocommerce产品
问题描述:
我想在购买某种产品(本例中为礼品卡)时停止应用任何优惠代码。购物车中的其他物品应不受影响,并且已将折扣码应用于其中。禁止将优惠券和折扣应用到购物车中已定义的Woocommerce产品
我知道这可以在单独的优惠券创建页面上完成,但是已经有数百个优惠券,我希望能够自动应用这些优惠券。
所有帮助非常感谢!
这是我发现的最接近的代码,但它涉及到插件而不是一般的woocommerce。
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;
}
答
要做到这一点,应通过优惠券设置限制经典的简单&有效的方法:
但是当你有很多的优惠券这里是一个自定义功能批量更新优惠券“排除产品”限制:
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();
的代码放在你的活跃儿童主题(或主题)的function.php文件或也以任何插件文件。
此代码已经过测试并可正常工作。
USAGE
1)制作一个数据库备份(或尤其是
wp_postmeta
表)。
2)将您的产品ID(或产品ID)设置在数组中(无折扣)。
3)粘贴代码function.php
您活动主题的文件并保存它。
4)在管理员帐户下,浏览您网站的任何页面。
5)评论或删除该代码。
6)您完成并且您可以查看一些优惠券查看结果。
答
我还没有尝试过上述的答案,因为Yith Gift Cards Plugin团队找到了我。对于任何寻找与Yith插件直接相关的答案的人来说,这里是代码...
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');
}
}
}