【问题标题】:Free shipping for the 100 first customers in WoocommerceWoocommerce 的前 100 位客户免运费
【发布时间】:2017-08-04 14:39:52
【问题描述】:

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

一旦达到 100 位第一批客户的限制,则将应用标准运费。

这可能吗?我该怎么做?

【问题讨论】:

    标签: php wordpress woocommerce checkout shipping


    【解决方案1】:

    下面是一个简单的方法,可以使用下面的 2 个挂钩函数:

    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+

    【讨论】:

      猜你喜欢
      • 2017-01-08
      • 2019-07-29
      • 2020-11-05
      • 2019-10-11
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 2018-08-09
      相关资源
      最近更新 更多