2017-08-04 89 views

回答

1

下面是一个简单的下面将在2上钩功能做这个工作的方式:

  1. 汽车到购物车添加优惠券代码启用1号100个客户“免费送货”选项。
  2. 当“免运费”可用时隐藏其他运输方式。

但你必须:

  1. 在WooCommerce>设置>航运,每个出货区设置了 “免费送货” 的方法,并选择了这个选项之一:
    • 一有效的免费送货优惠券
    • 最低订单金额或优惠券
  2. 之前在WooCommerce>优惠券使用以下设置设置特殊优惠券代码:
    • 常规>折扣类型:固定车
    • 常规>金额:0
    • 通用>允许免费送货:启用
    • 使用限制>每优惠券使用限制:100
    • 使用限制>每个用户使用限制:1

下面是代码:

// Auto apply "free_shipping" coupon for first hundred 
add_action('woocommerce_before_calculate_totals', 'auto_apply_free_shipping_coupon_first_hundred', 10, 1); 
function auto_apply_free_shipping_coupon_first_hundred($cart_object) { 

    // HERE define your free shipping coupon code 
    $coupon_code = 'summer';// 'freeship100'; 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    // Get an instance of the WC_Coupon object 
    $coupon_obj = new WC_Coupon($coupon_code); 

    // After 100 usages exit 
    if($coupon_obj->get_usage_count() > 100) return; 

    // Auto-apply "free shipping" coupon code 
    if (! $cart_object->has_discount($coupon_code) && is_cart()){ 
     $cart_object->add_discount($coupon_code); 
     wc_clear_notices(); 
     wc_add_notice(__('You have win Free shipping for the first 100 customers'), 'notice'); 
    } 
} 

// Hide Others Shipping methods when "Free shipping is available 
add_filter('woocommerce_package_rates', 'hide_others_when_free_shipping_is_available', 100); 
function hide_others_when_free_shipping_is_available($rates) { 
    $free = array(); 
    foreach ($rates as $rate_id => $rate) { 
     if ('free_shipping' === $rate->method_id) { 
      $free[ $rate_id ] = $rate; 
      break; 
     } 
    } 
    return ! empty($free) ? $free : $rates; 
} 

代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。

此代码已经过测试,适用于WooCommerce版本3+

相关问题