1

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;
}
Bliss Guy
  • 69
  • 7
  • About 1. why not put products you want to hide either out of stock go to woo settings and set it to hide products out of stock https://prnt.sc/1y5agdp . Another option is on product page in Catalog visibility you can hide products https://prnt.sc/1y5aqzj . Avoid complicating your shop with code that will do the same. – Snuffy Nov 02 '21 at 08:59
  • Products that are out of stock are set to back order. So hiding them is not an option. Hiding out of stock products also hurts SEO – Bliss Guy Nov 02 '21 at 22:10

2 Answers2

1

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

}
Dave S
  • 614
  • 4
  • 6
  • Thanks for the response, can you please help me with the changes as my knowledge of php is very basic. – Bliss Guy Nov 01 '21 at 22:24
  • Thanks for the updated code. It works perfectly by showing to only the specified taxonomy however i would like the ***$del_day*** to be in the format eg ***4 Nov & 7 Nov*** i.e 3 days difference from the current day. So assuming the customer is viewing the page on the 3 Nov, it will show ***Ready for delivery between 4 Nov & 7 Nov when you order within 5h:30mins(hours left before the day ends)*** – Bliss Guy Nov 05 '21 at 11:29
  • Okay i think am getting close to what i need. I created a new variable ***$del_day2*** and updated the old variable and it now looks like `$del_day = date( "j M", strtotime( "today + 1 day" ) ); $del_day2 = date( "j M", strtotime( "today + 4 day" ) );` Am now struggling to get ***$order_by*** to show the hours countdown left before the day will end. – Bliss Guy Nov 05 '21 at 11:49
  • I'm not sure exactly what you mean without seeing the code. Maybe you need to create a new question or maybe something like this will help: [Calculate difference (in hours) between two times in PHP](https://stackoverflow.com/questions/29664870/calculate-difference-in-hours-between-two-times-in-php) – Dave S Nov 05 '21 at 13:27
  • I figured how to output the hour and minutes left before 12am using `$order_by = gmdate("g:i", strtotime("tomorrow") - time());` How do i show the output time in the format ***7hrs 4mins*** instead of ***7:04*** ? – Bliss Guy Nov 05 '21 at 17:04
0
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

Bliss Guy
  • 69
  • 7