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;
I currently have a custom taxonomy (Available now) and would like to modify the code to show for only products with that taxonomy
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].)
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; }
Advertisement
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