Search code examples
phpwordpresswoocommercehook-woocommerce

woocommerce shipping rate option on specific days and cut-off time


I have three shipping rates,

  • Saturday Delivery - flat_rate:34 - £11.25
  • Saturday Delivery - flat_rate:35 - £5 when order is =>£75
  • Free Next Day Delivery - shipping_method_0_free_shipping19

Free Next Day Delivery is available all week.

I want flat_rate:34 to be visible on Thursday (all day) and Friday, and be available only until 12:30 on Friday, thereafter the "cut-off" the rate is hidden. If the total order amount is => £75.00 then flat_rate:35 is available and _rate:34 is hidden.

I want this option to be alongside the standard free next day delivery, currently if the order total is => 75 only the free_shipping option shows ... I want that AND the Saturday option (rate:35) to show.

How can I expand on the code below to the achieve the above?

Somewhere I am to include this

  ( WC()->cart->subtotal => $threshold ) 

,

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_time', 10, 2 );
function hide_shipping_method_based_on_time( $rates, $package )
{

date_default_timezone_set('Europe/London');
$threshold = 75;
$shipping_rate_id_1 = 'flat_rate:34';
$shipping_rate_id_2 = 'flat_rate:35';

// disabled on Mon/Tue/Wed/Sat/Sun 
if ( array_key_exists( $shipping_rate_id_1, $shipping_rate_id_2, $rates ) 
&& ( in_array(date('N'), [1,2,3,6,7]) ) ) {
    unset( $rates[$shipping_rate_id_1] );
    unset( $rates[$shipping_rate_id_2] );
}

// Cut off 12:30 on Friday's

if ( array_key_exists( $shipping_rate_id_1, $shipping_rate_id_2, $rates ) date('N') = 5 && date('H') => 12:30 ) {
    unset($rates[$shipping_rate_id_1]); // remove it
    unset($rates[$shipping_rate_id_2]); // remove it
}
return $rates; 
}

Solution

  • <?php
    add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_time', 10, 2 );
    function hide_shipping_method_based_on_time( $rates, $package )
    {
    
        date_default_timezone_set('Europe/London');
        $threshold = 75;
        $shipping_rate_id_1 = 'flat_rate:34';
        $shipping_rate_id_2 = 'flat_rate:35';
        $free_shipping_id = 'shipping_method_0_free_shipping19';
        $cart_total = WC()->cart->subtotal;
    
        // Disable on Mon/Tue/Wed/Sat/Sun
        if ( array_key_exists( $shipping_rate_id_1, $shipping_rate_id_2, $rates ) && ( in_array(date('N'), [1,2,3,6,7]) ) ) {
            unset( $rates[$shipping_rate_id_1] );
            unset( $rates[$shipping_rate_id_2] );
        }
        
        // Cut off 12:30 on Friday's
        if ( array_key_exists( $shipping_rate_id_1, $shipping_rate_id_2, $rates ) date('N') = 5 && date('H') => 12:30 ) {
            unset($rates[$shipping_rate_id_1]); // remove it
            unset($rates[$shipping_rate_id_2]); // remove it
        }
        
        // Show flat_rate:35 if cart total is >= £75
        if ( $cart_total >= $threshold && array_key_exists( $shipping_rate_id_2, $rates ) ) {
            unset( $rates[$shipping_rate_id_1] );
        }
    
        // Show both free_shipping and flat_rate:35 if cart total is >= £75
        if ( $cart_total >= $threshold && array_key_exists( $free_shipping_id, $rates ) ) {
            $rates[$shipping_rate_id_2]->cost = 0;
        }
    
        return $rates; 
    }
    

    Check this out

    For change in it, let me know in a comment