Woocommerce的100个首批客户免费送货



我正在尝试在Woocommerce中找到一种方法,允许前100名客户免费送货作为促销活动。

一旦达到 100 个首批客户的限制,将应用标准运输。

这可能吗?我该怎么做?

这里有一个简单的方法,可以使用下面的 2 个钩子函数来做到这一点:

  1. 自动将优惠券代码添加到购物车,并为1st 100客户启用"免费送货"选项。
  2. 在"免费送货"可用时隐藏其他运输方式。

但您将拥有:

在WooCommerce>设置>运输中,
  1. 为每个运输区域设置"免费送货"方法,然后选择以下选项之一:
    • 有效的免费送货优惠券
    • 最低订单金额或优惠券
  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', 20, 1 );
function auto_apply_free_shipping_coupon_first_hundred( $cart ) {
// HERE define  your free shipping coupon code
$coupon_code = 'summer';// 'freeship100';
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Get an instance of the WC_Coupon object
$coupon = new WC_Coupon( $coupon_code );
// After 100 usages exit
if( $coupon->get_usage_count() > 100 ) return;
// Auto-apply "free shipping" coupon code
if ( ! $cart->has_discount( $coupon_code ) && is_cart() ){
$cart->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;
}

代码进入函数.php活动子主题(或主题(的文件或任何插件文件中。

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

最新更新