skip to Main Content

I am currently using the snippet below to show estimated delivery on my single product pages. I need help making a few changes to the code;

  1. I currently have a custom taxonomy (Available now) and would like to
    modify the code to show for only products with that taxonomy

  2. Change the output notice to show eg. (Ready for delivery between 4
    Nov & 7 Nov when you order within [hours left before end of day].)

  3. Hide the notice when when the item is out of stock


add_action( 'woocommerce_before_add_to_cart_form', 'delivery_estimate' );
    
function delivery_estimate() {
   date_default_timezone_set( 'Europe/Tallinn' );  
    
   // if FRI/SAT/SUN delivery will be MON
   if ( date( 'N' ) >= 5 ) {
      $del_day = date( "l jS F", strtotime( "next tuesday" ) );
      $order_by = "Monday";
   } 
    
   // if MON/THU after 4PM delivery will be day after tomorrow
   elseif ( date( 'H' ) >= 16 ) {
      $del_day = date( "l jS F", strtotime( "tomorrow + 1 day" ) );
      $order_by = "tomorrow";
   } 
    
   // if MON/THU before 4PM delivery will be TOMORROW
   else {
      $del_day = date( "2 jS F", strtotime( "tomorrow" ) );
      $order_by = "today";
   }
 
   $html = "<br><div class='woocommerce-message' style='clear:both'>Order by 4PM {$order_by} for delivery on {$del_day}</div>";
    
   echo $html;
}

2

Answers


  1. Chosen as BEST ANSWER
    add_action( 'woocommerce_before_add_to_cart_form', 'delivery_estimate' );
    
    function delivery_estimate() {
    
       global $product;
    
       if ( 'product' == $product->post_type ) {
    
           $terms = wp_get_post_terms( $product->id, 'shipment-status' );
    
           if ( $terms instanceof WP_Error ) {
               // Log error here or whatever you need to do
    
           } elseif ( ! empty( $terms ) ) {
    
               $term = $terms[ 0 ]; // assuming it is single-valued
    
               if ( 'available-in-ghana' == $term->slug ) {
    
                   date_default_timezone_set( 'Africa/Accra' );  
    
                   // if FRI/SAT/SUN delivery will be between +1 & +4 days
                   if ( date( 'N' ) >= 5 ) {
                      $del_day = date( "j M", strtotime( "today + 1 day" ) );
                      $del_day2 = date( "j M", strtotime( "today + 4 day" ) );
                      $hour = gmdate("g", strtotime("tomorrow") - time()) . hrs;
                      $minutes = gmdate("i", strtotime("tomorrow") - time()) . mins;
                    
                    } 
    
                   // if MON/THU after 4PM delivery will be day after tomorrow
                   elseif ( date( 'H' ) >= 16 ) {
                      $del_day = date( "j M", strtotime( "tomorrow + 1 day" ) );
                      $hour = gmdate("g", strtotime("tomorrow") - time()) . hrs;
                      $minutes = gmdate("i", strtotime("tomorrow") - time()) . mins;
                   } 
    
                   // if MON/THU before 4PM delivery will be TOMORROW
                   else {
                      $del_day = date( "j M", strtotime( "tomorrow" ) );
                      $hour = gmdate("g", strtotime("tomorrow") - time()) . hrs;
                      $minutes = gmdate("i", strtotime("tomorrow") - time()) . mins;
                   }
    
                   $html = "<br><div class='woocommerce-message' style='clear:both'>Order within the next $hour $minutes for delivery between {$del_day} & {$del_day2}</div>";
    
                   echo $html;
    
            } // endif
    
        } // endif
    
    }}
    

    OUTPUT

    enter image description here


  2. If you use WooCommerce’s global $product you can get the taxonomy terms for the current product. Then you should be able to display the right messages.

    Your code will look something like this:

    add_action( 'woocommerce_before_add_to_cart_form', 'delivery_estimate' );
    
    function delivery_estimate() {
    
       global $product;
    
       if ( 'YOUR_POST_TYPE' == $product->post_type ) {
    
           $terms = wp_get_post_terms( $product->id, 'YOUR_TAXONOMY_NAME' );
    
           if ( $terms instanceof WP_Error ) {
               // Log error here or whatever you need to do
    
           } elseif ( ! empty( $terms ) ) {
    
               $term = $terms[ 0 ]; // assuming it is single-valued
    
               if ( 'YOUR_AVAILABLE_NOW_SLUG' == $term->slug ) {
    
                   date_default_timezone_set( 'Europe/Tallinn' );  
    
                   // if FRI/SAT/SUN delivery will be MON
                   if ( date( 'N' ) >= 5 ) {
                      $del_day = date( "l jS F", strtotime( "next tuesday" ) );
                      $order_by = "Monday";
                   } 
    
                   // if MON/THU after 4PM delivery will be day after tomorrow
                   elseif ( date( 'H' ) >= 16 ) {
                      $del_day = date( "l jS F", strtotime( "tomorrow + 1 day" ) );
                      $order_by = "tomorrow";
                   } 
    
                   // if MON/THU before 4PM delivery will be TOMORROW
                   else {
                      $del_day = date( "2 jS F", strtotime( "tomorrow" ) );
                      $order_by = "today";
                   }
    
                   $html = "<br><div class='woocommerce-message' style='clear:both'>Order by 4PM {$order_by} for delivery on {$del_day}</div>";
    
                   echo $html;
    
            } // endif
    
        } // endif
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search