【问题标题】:Disable coupons & discounts from applying to defined Woocommerce products in cart禁止将优惠券和折扣应用于购物车中定义的 Woocommerce 产品
【发布时间】:2018-03-24 16:25:15
【问题描述】:

我想停止在购买特定产品(在本例中为礼品卡)时应用任何折扣代码。购物车中的其他商品应该不受影响,并且应用了折扣代码。

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

非常感谢所有帮助!

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

【问题讨论】:

    标签: php sql woocommerce cart coupon


    【解决方案1】:

    经典的简单有效的方法应该是通过优惠券设置限制

    但由于您有很多优惠券,这是一个自定义功能批量更新优惠券“排除产品”限制

    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) 大功告成,您可以查看一些优惠券来查看结果。

    【讨论】:

      【解决方案2】:

      由于 Yith 礼品卡插件团队回复了我,我还没有尝试上述答案。对于正在寻找与 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' );
      
        }
       }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-12
        • 2020-11-04
        • 2011-06-21
        • 1970-01-01
        • 1970-01-01
        • 2021-03-18
        • 2021-06-10
        • 1970-01-01
        相关资源
        最近更新 更多