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

时间:2017-10-12 16:26:18

标签: php sql woocommerce cart coupon

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

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

非常感谢所有帮助!

这是我发现的最接近的代码,但它与插件而不是一般的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 :(得分:0)

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

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)您已完成并且您可以查看一些优惠券以查看结果。

答案 1 :(得分: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' );

  }
 }

}