【问题标题】:Shipping methods based on a defined date threshold in WooCommerce基于 WooCommerce 中定义的日期阈值的运输方法
【发布时间】:2019-02-27 04:50:18
【问题描述】:

我想在 WooCommerce 中启用两种运输方式,例如如果用户在特定日期之前订购,那么我想启用第一种运输方式,当用户在特定日期之后订购时,我想启用第二种运输方式。谁能告诉我是否有任何插件或代码来执行此功能?

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce shipping-method


    【解决方案1】:

    以下代码将根据定义的日期阈值启用不同的运输方式。

    您必须在函数中定义您的设置:
    - 商店时区
    - 2 种运输方式的费率 ID (如“flat_rate:12”格式)
    - 日期阈值

    代码:

    add_filter( 'woocommerce_package_rates', 'free_shipping_disable_flat_rate', 100, 2 );
    function free_shipping_disable_flat_rate( $rates, $package ) {
    
        ## ----- YOUR SETTINGS HERE BELOW  ----- ##
    
        date_default_timezone_set('Europe/London'); // <== Set the time zone (http://php.net/manual/en/timezones.php)
    
        $shippping_rates = ['flat_rate:12', 'flat_rate:14']; // <== Set your 2 shipping methods rate IDs
        $defined_date    = "2019-03-05";                     // <== Set your date threshold
    
        ## ------------------------------------- ##
    
        $now_timestamp  = strtotime("now"); // Current timestamp in seconds
        $date_timestamp = strtotime($defined_date); // Targeted timestamp threshold
    
        // 1. BEFORE the specified date (with 1st shipping method rate ID)
        if ( array_key_exists( $shippping_rates[0], $rates ) && $now_timestamp > $date_timestamp ) {
            unset($rates[$shippping_rates[0]]); // Remove first shipping method
        }
        // 2. AFTER the specified date included (with 2nd shipping method rate ID)
        elseif ( array_key_exists( $shippping_rates[1], $rates ) && $now_timestamp <= $date_timestamp ) {
            unset($rates[$shippping_rates[1]]); // Remove Second shipping method
        }
    
        return $rates;
    }
    

    代码在您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    要使其正常工作,您需要刷新发货缓存数据:
    1) 首先,将此代码粘贴并保存到您的 function.php 文件中。
    2) 在配送设置中,进入配送区域,然后禁用配送方式并“保存”并重新启用并“保存”。 你已经完成了。

    要获得正确的送货方式费率 ID,请使用浏览器工具(在购物车或结帐页面中)检查其单选按钮代码,并使用 value 属性数据,例如:

    <input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_flat_rate12" 
    value="flat_rate:12" class="shipping_method" checked="checked">
    

    …所以这里是 flat_rate:12 in value="flat_rate:12"

    【讨论】:

    • 谢谢@Loic 你能告诉我如何通过 WordPress 管理员或以编程方式创建这两种运输方式 'flat_rate:12', 'flat_rate:14'
    • @R1222 您必须首先创建 2 个需要运输的方法(在每个方法中设置正确的金额)... 完成后,检查购物车页面上的运输方式单选按钮以获取相应的运费 ID。您将在功能代码中设置这两个运费 ID...
    猜你喜欢
    • 1970-01-01
    • 2021-06-07
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2017-02-12
    • 2021-10-06
    相关资源
    最近更新 更多