Support » Plugin: WooCommerce » Restrict postage days

  • SRD75

    (@srd75)


    Hi Folks,

    We are using postcodes to set shipping zones and prices.

    For 2 out of 3 shipping zones, we’d like to restrict shipping to Monday to Saturday only.

    Is there a way to configure this in WooCommerce?

    Thanks,
    Steve

Viewing 2 replies - 1 through 2 (of 2 total)
  • tomasz_bednarek

    (@tomasz_bednarek)

    Hi Steve,

    you may add additional code, a filter to hide 2 shipping methods for Sundays:
    https://stackoverflow.com/questions/23701467/woocommerce-hide-shipping-method-based-on-shipping-class

    you may adjust above code with your custom one to check if it’s Sunday:
    https://www.php.net/manual/en/function.date.php
    for example:
    https://stackoverflow.com/questions/4969688/dates-in-php-sunday-and-saturday

    SRD75

    (@srd75)

    Thanks Tomasz.

    From the tutorials, I’ve created the following 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, I don’t understand the meaning of:

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

    Can you explain what these two are?

    I need to hide zone_id=1, zone_id=3 on Sundays. I don’t have any Shipping Classes, and don’t understand what they are.

    Thanks,
    Steve

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.