在 WooCommerce 中每周的特定日期提供免费送货服务 [英] Make free shipping available on specific days of the week in WooCommerce

查看:21
本文介绍了在 WooCommerce 中每周的特定日期提供免费送货服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a way to give free shipping on all orders based on weekday of the order. So if someone places an order (let's say on Monday) it will give him free shipping no matter what type of order quantity/amount/etc.

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

I've tried to hook up something from different other tutorials but I can't seem to get the part where I change the free_shipping limit + not sure it works since it's incomplete.

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');

非常感谢任何帮助,因为我有点坚持这个.

Any help is very appreciated since I'm kind of stuck with this.

推荐答案

要在一周中的特定日期免费送货可用,它需要不同的挂钩来<强>绕过免费送货限制(如果存在).

To make free shipping available on specific days of the week, it requires a different hook to bypass the free shipping limitations (if they exist).

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

We use date() function with "w" parameter that gives an integer from 0 (Sunday) to 6 (Saturday):

// 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;
}

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

To hide other shipping methods when free shipping is available, you can use additionally the following:

// 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 文件中.经测试有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于在 WooCommerce 中每周的特定日期提供免费送货服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆