禁用优惠券和应用于购物车中定义的Woocommerce产品的折扣 [英] Disable coupons & discounts from applying to defined Woocommerce products in cart

查看:111
本文介绍了禁用优惠券和应用于购物车中定义的Woocommerce产品的折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在购买某种产品(在这种情况下为礼品卡)时,我想停止应用任何折扣代码。购物车中的其他物品应不受影响,并应用了折扣代码。

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.

非常感谢所有帮助!

这是我找到的最接近的代码,但通常与插件而不是woocommerce有关。

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;
}


推荐答案

我没有尝试过以上答案是由Yith Gift Cards Plugin团队返回给我的。对于寻找与Yith插件直接相关的答案的人,这里是代码...

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' );

  }
 }

}

这篇关于禁用优惠券和应用于购物车中定义的Woocommerce产品的折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆