【发布时间】:2017-08-04 14:39:52
【问题描述】:
我正在尝试在 Woocommerce 中找到一种方法,允许前 100 名客户免费送货作为促销活动。
一旦达到 100 位第一批客户的限制,则将应用标准运费。
这可能吗?我该怎么做?
【问题讨论】:
标签: php wordpress woocommerce checkout shipping
我正在尝试在 Woocommerce 中找到一种方法,允许前 100 名客户免费送货作为促销活动。
一旦达到 100 位第一批客户的限制,则将应用标准运费。
这可能吗?我该怎么做?
【问题讨论】:
标签: php wordpress woocommerce checkout shipping
下面是一个简单的方法,可以使用下面的 2 个挂钩函数:
但你会有:
代码如下:
// 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;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。
此代码经过测试,适用于 WooCommerce 版本 3+
【讨论】: