根据 WooCommerce 中的白天时间隐藏特定的运输方式 [英] Hide specific shipping method depending on day time in WooCommerce

查看:26
本文介绍了根据 WooCommerce 中的白天时间隐藏特定的运输方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建一个 PHP 代码片段,并将其添加到我的 functions.php 文件中,但我不确定如何继续.

两年前有人问过同样的问题(

解决方案

这很简单,使用 woocommerce_package_rates 过滤器钩子.

现在您需要找出您需要定位的运费 Id 参考是什么.为此,您将使用浏览器工具检查所需的运输方式单选按钮 ( field) 以获取如下所示的价值:

一旦你得到它,你将在下面的代码中进行设置:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_time', 10, 2 );函数 hide_shipping_method_based_on_time( $rates, $package ){//设置默认时区 (http://php.net/manual/en/timezones.php)date_default_timezone_set('欧洲/伦敦');//这里设置你的运费 ID$shipping_rate_id = 'local_pickup:13';//当此送货方式可用且在上午 11 点之后if ( array_key_exists( $shipping_rate_id, $rates ) && date('H') > 11 ) {未设置($rates[$shipping_rate_id]);//去掉它}返回 $rates;}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

<块引用>

清除运输缓存:

  • 您需要清空购物车,以清除缓存的运输数据
  • 或者在运输设置中,您可以禁用/保存任何运输方式,然后启用返回/保存.

I'm looking to create a PHP code snippet that I would add to my functions.php file but I'm not sure how to proceed.

Someone asked the same question 2 years ago (Woocommerce - disable certain shipping methods based on time of day) but finally opted for a plugin that does that. I already use add-ons for detailed delivery options and I don't want to change or add another one just for this simple function if something can be done easily with php.

So I'm looking to disable a shipping method if the client order after 11AM (website time zone).

So I have this code for now :

if (date('H') > 11) { 
     $shippingmethod1.disable();
}

Can someone give me the proper code to make it work?

解决方案

This is quiet simple using woocommerce_package_rates filter hook.

Now you need to find out what is the shipping rate Id reference that you need to target. For that you will inspect the desired shipping method radio button (<input> field) with your browser tools to get value as shown below:

Once you get it you will set that in the code below:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_time', 10, 2 );
function hide_shipping_method_based_on_time( $rates, $package )
{
    // Set your default time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/London');
    
    // Here set your shipping rate Id
    $shipping_rate_id = 'local_pickup:13';

    // When this shipping method is available and after 11 AM
    if ( array_key_exists( $shipping_rate_id, $rates ) && date('H') > 11 ) {
        unset($rates[$shipping_rate_id]); // remove it
    }
    return $rates;
}

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

Clearing shipping caches:

  • You will need to empty your cart, to clear cached shipping data
  • Or In shipping settings, you can disable / save any shipping method, then enable back / save.

这篇关于根据 WooCommerce 中的白天时间隐藏特定的运输方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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