【问题标题】:Make free shipping available on specific days of the week in WooCommerce在 WooCommerce 中每周的特定日期提供免费送货服务
【发布时间】:2024-01-07 19:31:01
【问题描述】:

我正在寻找一种方法,根据订单的工作日为所有订单提供免费送货服务。因此,如果有人下订单(比如说星期一),无论订单数量/金额/等类型如何,他都会免费送货。

我尝试从其他不同的教程中获取一些东西,但我似乎无法获得我更改 free_shipping 限制的部分 + 由于它不完整,因此不确定它是否有效。

function free_shipping_day( $rates, $package ) {

// valid days
$valid_days = array('Mon');

// current day
$today = date ( 'D' );

// check if it's valid day
if( in_array($today, $valid_day) ){
    // clear other shipping methods allow only free
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free[ $rate_id ] = $rate;

        }

    }

    // set free_shipping limit to 0


    // show notice
    wc_add_notice( __( "Shipping is free today!" ), 'notice');
}
}

add_action('woocommerce_package_rates', 'free_shipping_day');

任何帮助都非常感谢,因为我有点坚持这一点。

【问题讨论】:

    标签: php wordpress date woocommerce shipping-method


    【解决方案1】:

    要在一周中的特定日子提供免费送货服务,需要不同的钩子绕过免费送货限制 (如果它们存在)

    我们使用带有 "w" 参数的 date() 函数,它给出了从 0(星期日)到 6 的整数(星期六):

    // Enable free shipping on specific days of the week
    add_filter( 'woocommerce_shipping_free_shipping_is_available', 'enable_free_shipping_for_specific_week_days', 10, 3 );
    function enable_free_shipping_for_specific_week_days( $is_available, $package, $shipping_method ) {
        // Free shipping is available on mondays and wednesdays for example
        if( in_array( date('w'), [ 1, 3 ] ) ) {
            return true;
        }
        return $is_available;
    }
    

    要在免费送货时隐藏其他送货方式,您可以另外使用以下方式:

    // Hide other shipping methods when free shipping is available
    add_filter( 'woocommerce_package_rates', 'hide_other_shipping_methods_when_free_shipping_is_available', 100, 2 );
    function hide_other_shipping_methods_when_free_shipping_is_available( $rates, $package ) {
        $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 文件中。经过测试并且可以工作。

    【讨论】:

    • 我还想将当天的免运费限制减少到 0,这样任何订单都可以免运费。
    • 确实,它设置了免费送货真实、有趣的方法,非常感谢您的帮助!
    【解决方案2】:

    在活动主题的functions.php中添加以下代码sn-p -

    function enable_free_shipping_for_days( $rates ) {
        $free = array();
        // valid days
        $valid_days = array('Mon');
        if( !in_array( date('D'), $valid_days ) ) return $rates;
    
        foreach ( $rates as $rate_id => $rate ) {
            if ( 'free_shipping' !== $rate->method_id ) continue;
            $free[ $rate_id ] = $rate;
        }
        if( $free ) wc_add_notice( __( "Shipping is free today!" ), 'notice');
        return ( $free ) ? $free : $rates;
    }
    add_filter( 'woocommerce_package_rates', 'enable_free_shipping_for_days', 99 );
    

    【讨论】:

    • 这是一个经过测试的代码。您是否已通过数组中的“星期三”。如果是这样,那么只需通过更新购物车或从购物车中删除商品来刷新购物车片段一次。并检查。
    • 对不起,它不起作用,在我的父主题的函数中添加代码,清除缓存,尝试隐身,但没有成功(使用这个:$valid_days = array('Thu'); )
    最近更新 更多