skip to Main Content

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!

2

Answers


  1. If you want to use php for logic and hook it to needed place in template

    function add_est_time(){
    
       //from 0 to 23
       $current_hour = date( 'G', time() );
       //from 0 to 6
       $current_day = date( 'w', time() );
    
       $message = __('3-5 Working Days', 'textdomain'); 
    
       if ( $current_hour >= 12) {
         $message = __('4-5 Working Days', 'textdomain');
       }
    
       if ($current_day >= 5) {
         $message = __('3-5 Working Days from Monday', 'textdomain');
       }
    
       echo $message;
    }
    
    //hook to needed place
    add_action( 'woocommerce_before_add_to_cart_button', 'add_est_time' ); 
    

    Also you can use JS and replace needed text with this logic.

    Login or Signup to reply.
  2. 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' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search