簡體   English   中英

為Woocommerce中的100個首批客戶免費送貨

[英]Free shipping for the 100 first customers in Woocommerce

我試圖在Woocommerce中找到一種方法,以允許促銷活動的前100個客戶免費送貨。

一旦達到100個首批客戶的限制,將采用標准運輸。

這可能嗎? 我怎樣才能做到這一點?

這是使用下面的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', 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM