[英]Free shipping for the 100 first customers in Woocommerce
我試圖在Woocommerce中找到一種方法,以允許促銷活動的前100個客戶免費送貨。
一旦達到100個首批客戶的限制,將采用標准運輸。
這可能嗎? 我怎樣才能做到這一點?
這是使用下面的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+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.