1

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.

Help appreciated please.

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
Steve
  • 2,066
  • 13
  • 60
  • 115
  • A Shipping Zone is a geographical area to which you ship items. Shipping methods can be 'free shipping', 'local pickup', etc... Shipping classes can be used to group products of similar type and used by some shipping methods, such as Flat Rate Shipping, to provide different rates to different classes of product. – 7uc1f3r Jul 02 '20 at 13:34
  • So is believe that you want to hide certain shipping methods on sunday opposite shipping zones? or am I wrong? can you clarify this? – 7uc1f3r Jul 02 '20 at 14:22
  • @7uc1f3r, it is just shipping zones with id of 1 and 3 that I want to prevent from being available on a Sunday. – Steve Jul 03 '20 at 03:38

1 Answers1

0

Code contains

  • On a specific day
  • Unset $rates[$rate_key] for certain zone IDs

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 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50