如何从一周的某些日子限制 WooCommerce 运输区 [英] How to restrict WooCommerce shipping zone from certain days of the week

查看:20
本文介绍了如何从一周的某些日子限制 WooCommerce 运输区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要隐藏 WooCommerce 运输 zone_id=1 &周日发货 zone_id=3.

I need to hide WooCommerce shipping zone_id=1 & shipping zone_id=3 on Sundays.

我偶然发现了这个答案,并想出了这个代码:

I've stumbled upon this answer, and come up with this code:

// Hide $15 & $25 shipping rates on Sunday

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

但是,我不知道如何自定义的是:

However, the bit I don't know how to customize is:

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

我没有任何运输类,也不明白它们是什么.可能需要替换更多代码以替代运输区域的隐藏运输类别,但我不知道该代码是什么.

I don’t have any Shipping Classes, and don’t understand what they are. More code will probably need to be replaced to substitute hiding shipping classes for shipping zones, but I don't know what that code is.

感谢帮助.

推荐答案

代码包含

  • 在特定的一天
  • 为某些区域 ID 取消设置 $rates[$rate_key]

在代码中添加注释和解释

Comment with explanation added to the code

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

这篇关于如何从一周的某些日子限制 WooCommerce 运输区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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