2017-10-12 118 views
1

我想在購買某種產品(本例中爲禮品卡)時停止應用任何優惠代碼。購物車中的其他物品應不受影響,並且已將折扣碼應用於其中。禁止將優惠券和折扣應用到購物車中已定義的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; 
} 

回答

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文件或也以任何插件文件。

此代碼已經過測試並可正常工作。


USAGE

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

    } 
} 

} 
相關問題