亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何限制一周中某些日子的 WooCommerce 送貨區域

如何限制一周中某些日子的 WooCommerce 送貨區域

PHP
繁星coding 2023-07-08 17:47:41
我需要在周日隱藏 WooCommerce Shipping zone_id=1 和 Shipping zone_id=3。提出了這段代碼:// Hide $15 & $25 shipping rates on Sundayadd_filter('woocommerce_package_rates', 'amano_hide_shipping_method_on_sunday', 10, 2);function amano_hide_shipping_method_on_sunday($available_shipping_methods, $package) {$date = date("1");$is_sunday = date('l', $date) == 'Sunday';if($is_sunday) {? ? $hide_when_shipping_class_exist = array(? ? ? ? 42 => array(? ? ? ? ? ? 'free_shipping'? ? ? ? )? ? );? ??? ? $hide_when_shipping_class_not_exist = array(? ? ? ? 42 => array(? ? ? ? ? ? 'wf_shipping_ups:03',? ? ? ? ? ? 'wf_shipping_ups:02',? ? ? ? ? ? ?'wf_shipping_ups:01'? ? ? ? ),? ? ? ? 43 => array(? ? ? ? ? ? 'free_shipping'? ? ? ? )? ? );? ? $shipping_class_in_cart = array();? ? foreach(WC()->cart->cart_contents as $key => $values) {? ? $shipping_class_in_cart[] = $values['data']->get_shipping_class_id();? ? }? ? foreach($hide_when_shipping_class_exist as $class_id => $methods) {? ? ? ? if(in_array($class_id, $shipping_class_in_cart)){? ? ? ? ? ? foreach($methods as & $current_method) {? ? ? ? ? ? ? ? unset($available_shipping_methods[$current_method]);? ? ? ? ? ? }? ? ? ? }? ? }? ? foreach($hide_when_shipping_class_not_exist as $class_id => $methods) {? ? ? ? if(!in_array($class_id, $shipping_class_in_cart)){? ? ? ? ? ? foreach($methods as & $current_method) {? ? ? ? ? ? ? ? unset($available_shipping_methods[$current_method]);? ? ? ? ? ? }? ? ? ? }? ? }? ? return $available_shipping_methods;? ? }}然而,我不知道如何定制的是:? ? $hide_when_shipping_class_exist = array(? ? ? ? 42 => array(? ? ? ? ? ? 'free_shipping'? ? ? ? )? ? );? ? $hide_when_shipping_class_not_exist = array(? ? ? ? 42 => array(? ? ? ? ? ? 'wf_shipping_ups:03',? ? ? ? ? ? 'wf_shipping_ups:02',? ? ? ? ? ? ?'wf_shipping_ups:01'? ? ? ? ),? ? ? ? 43 => array(? ? ? ? ? ? 'free_shipping'? ? ? ? )? ? );我沒有任何運輸類別,也不明白它們是什么??赡苄枰鎿Q更多代碼來替換隱藏運輸區域的運輸類別,但我不知道該代碼是什么。請幫助表示贊賞。
查看完整描述

1 回答

?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

代碼包含

  • 在特定的一天

  • 未設置$rates[$rate_key]某些區域 ID

注釋并添加到代碼中的解釋

function filter_woocommerce_package_rates( $rates, $package ) {

    /* SETTINGS */

    

    // Zone ID's

    $hide_zones = array ( 1, 3 );

    

    $on_day = 'Sunday';

    

    /* END SETTINGS */

    

    // Shipping zone

    $shipping_zone = wc_get_shipping_zone( $package );

    

    // Get zone ID

    $zone_id = $shipping_zone->get_id();

    

    // set the default timezone to use. Available since PHP 5.1

    date_default_timezone_set('UTC');

    

    // l - A full textual representation of the day of the week

    $day_of_the_week = date( 'l' );


    // True

    if( $day_of_the_week == $on_day ) {

        // In array

        if ( in_array( $zone_id, $hide_zones ) ) {

            // Loop

            foreach ( $rates as $rate_key => $rate ) {

                // Unset

                unset( $rates[$rate_key] );

            }

        }

    }

    

    return $rates;

}

add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );


查看完整回答
反對 回復 2023-07-08
  • 1 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號