skip to Main Content

I’m looking for a way to give free shipping on all orders based on weekday of the order. So if someone places an order (let’s say on Monday) it will give him free shipping no matter what type of order quantity/amount/etc.

I’ve tried to hook up something from different other tutorials but I can’t seem to get the part where I change the free_shipping limit + not sure it works since it’s incomplete.

function free_shipping_day( $rates, $package ) {

// valid days
$valid_days = array('Mon');

// current day
$today = date ( 'D' );

// check if it's valid day
if( in_array($today, $valid_day) ){
    // clear other shipping methods allow only free
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free[ $rate_id ] = $rate;

        }

    }

    // set free_shipping limit to 0


    // show notice
    wc_add_notice( __( "Shipping is free today!" ), 'notice');
}
}

add_action('woocommerce_package_rates', 'free_shipping_day');

Any help is very appreciated since I’m kind of stuck with this.

2

Answers


  1. Add the follows code snippet in your active theme’s functions.php –

    function enable_free_shipping_for_days( $rates ) {
        $free = array();
        // valid days
        $valid_days = array('Mon');
        if( !in_array( date('D'), $valid_days ) ) return $rates;
    
        foreach ( $rates as $rate_id => $rate ) {
            if ( 'free_shipping' !== $rate->method_id ) continue;
            $free[ $rate_id ] = $rate;
        }
        if( $free ) wc_add_notice( __( "Shipping is free today!" ), 'notice');
        return ( $free ) ? $free : $rates;
    }
    add_filter( 'woocommerce_package_rates', 'enable_free_shipping_for_days', 99 );
    
    Login or Signup to reply.
  2. To make free shipping available on specific days of the week, it requires a different hook to bypass the free shipping limitations (if they exist).

    We use date() function with “w” parameter that gives an integer from 0 (Sunday) to 6 (Saturday):

    // Enable free shipping on specific days of the week
    add_filter( 'woocommerce_shipping_free_shipping_is_available', 'enable_free_shipping_for_specific_week_days', 10, 3 );
    function enable_free_shipping_for_specific_week_days( $is_available, $package, $shipping_method ) {
        // Free shipping is available on mondays and wednesdays for example
        if( in_array( date('w'), [ 1, 3 ] ) ) {
            return true;
        }
        return $is_available;
    }
    

    To hide other shipping methods when free shipping is available, you can use additionally the following:

    // Hide other shipping methods when free shipping is available
    add_filter( 'woocommerce_package_rates', 'hide_other_shipping_methods_when_free_shipping_is_available', 100, 2 );
    function hide_other_shipping_methods_when_free_shipping_is_available( $rates, $package ) {
        $free = array();
    
        foreach ( $rates as $rate_id => $rate ) {
            if ( 'free_shipping' === $rate->method_id ) {
                $free[ $rate_id ] = $rate;
                break;
            }
        }
        return ! empty( $free ) ? $free : $rates;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search