Skip to content
Advertisement

Show estimated dispatch date after add to cart form in product page

I have a Woocommerce store and I would like to show my customers an estimated dispatch date below the add to cart form. The logic is

  1. If the customer places the order by 12PM IST, it would be dispatched by 3-5 Working Days.
  2. If the customer places the order after 12PM IST, it would be dispatched by 4-5 Working Days.
  3. If the customer places the order on weekends, It should automatically consider it as 3-5 Working Days from Monday.

I tried the businessbloomer’s code but my logic is a little complicated so I couldn’t get it to work!

Any help is appreciated!

Advertisement

Answer

Check the code below ( Tested and works fine). If you are having issues with incorrect time, see if the timezone is set correct. Based on your query I assume the timezone should be Kolkatta.

Goto Dashboard -> Settings -> General -> Timezone

function dispatch_message(){

    $current_time = current_time('D | H');
    $current_time = explode('|', $current_time );
    $current_day = isset( $current_time[0] ) ? trim( $current_time[0] ) : false;
    $current_hour = isset( $current_time[1] ) ? trim( $current_time[1] ) : false;
  
    $message = '4-5 Working Days.';
    
    if( in_array( $current_day, array( 'Fri', 'Sat', 'Sun' ) ) ){
        //Weekend
        $message = '3-5 Working Days from Monday.';

    }else if( $current_hour <= "12"){
        //Before 12pm IST
        $message = '3-5 Working Days.';
    }

    ?>
    <div style="clear: both"></div>
    <div style="margin-top: 10px;font-size: 17px;color: red;">
        <b>Est Dispatch Date:</b> <?php echo $message; ?>
    </div>
    <br>
    <?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'dispatch_message' ); 

UPDATE

You can make changes to the above code inorder to hide the dispatched date for certain categories. Add the if condition as the first line of code as shown below and return inorder to skip the remaining part of function.

function dispatch_message(){

    if ( is_product() && has_term( array( 'dress', 'clothing' ), 'product_cat' ) ){
        return;
    }
    
    $current_time = current_time('D | H');
    $current_time = explode('|', $current_time );
    $current_day = isset( $current_time[0] ) ? trim( $current_time[0] ) : false;
    $current_hour = isset( $current_time[1] ) ? trim( $current_time[1] ) : false;

    $message = '4-5 Working Days.';

    if( in_array( $current_day, array( 'Fri', 'Sat', 'Sun' ) ) ){
        //Weekend
        $message = '3-5 Working Days from Monday.';

    }else if( $current_hour <= "12"){
        //Before 12pm IST
        $message = '3-5 Working Days.';
    }

    ?>
    <div style="clear: both"></div>
    <div style="margin-top: 10px;font-size: 17px;color: red;">
        <b>Est Dispatch Date:</b> <?php echo $message; ?>
    </div>
    <br>
    <?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'dispatch_message' );
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement