首页 文章

禁止在购物车中应用定义的Woocommerce产品的优惠券和折扣

提问于
浏览
1

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

我知道这可以在个人优惠券创建页面上完成,但是已经制作了数百张优惠券,我想要一种自动应用的方式 .

非常感谢所有帮助!

这是我发现的最接近的代码,但它与插件而不是一般的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;
}

2 回答

  • 0

    这种经典简单有效的方法应该是通过 coupon settings restrictions

    enter image description here

    但是因为你有很多优惠券这里是一个自定义功能来批量更新优惠券“排除产品”限制:

    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文件中,或者放在任何插件文件中 .

    此代码经过测试和运行 .


    用法1)进行数据库备份(或特别是wp_postmeta表) . 2)在阵列中设置您的产品ID(或产品ID)(无折扣) . 3)将该代码粘贴到活动主题的function.php文件中并保存 . 4)在管理员帐户下,浏览您网站的任何页面 . 5)注释或删除该代码 . 6)您已完成,您可以查看一些优惠券以查看结果 .

  • 0

    我没有尝试上述答案,因为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' );
    
      }
     }
    
    }
    

相关问题