Php 基于WooCommerce中定义的日期阈值的装运方法

Php 基于WooCommerce中定义的日期阈值的装运方法,php,wordpress,woocommerce,hook-woocommerce,shipping-method,Php,Wordpress,Woocommerce,Hook Woocommerce,Shipping Method,我想在WooCommerce中启用两种发货方式,比如如果用户在特定日期之前下单,那么我想启用第一种发货方式;如果用户在特定日期之后下单,那么我想启用第二种发货方式。有人能告诉我是否有任何插件或代码来实现这个功能吗 以下代码将根据定义的日期阈值启用不同的装运方法 您必须在函数中定义您的设置: -商店时区 -两种装运方法的费率ID(如“固定费率:12”格式) -日期阈值 守则: add_filter( 'woocommerce_package_rates', 'free_shipping_disab

我想在WooCommerce中启用两种发货方式,比如如果用户在特定日期之前下单,那么我想启用第一种发货方式;如果用户在特定日期之后下单,那么我想启用第二种发货方式。有人能告诉我是否有任何插件或代码来实现这个功能吗

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

您必须在函数中定义您的设置:
-商店时区
-两种装运方法的费率ID(如“固定费率: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;
}
…所以这里是
value=“flat\u rate:12”


谢谢@Loic你能让我知道我如何通过WordPress管理员或编程方式“flat_rate:12”、“flat_rate:14”@R1222创建这两种运送方法吗?你必须先创建你需要的两种运送方法(分别设置正确的数量)…一旦完成,检查购物车页面上的“装运方法”单选按钮,以获取相应的装运费率ID。您将在功能代码中设置这两个运费ID…
<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">