On a WordPress 5.6 website with WooCommerce 4.9/Storefront, I've modified my child theme's functions.php to display an estimated shipping date in the products description, as well as in the cart and order page, based on the product get_stock_quantity() and is_on_backorder() functions.
I have trouble doing the same in the order confirmation email, apparently because by the time I use these functions, the stock has already been decreased.
For now, the code looks like this, based on an answer on StackOverflow:
// Setting the email_is as a global variable
add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email ){
$GLOBALS['email_id_str'] = $email->id;
}
add_action( 'woocommerce_order_item_meta_end', 'order_item_meta_end', 10, 4 );
function order_item_meta_end( $item_id, $item, $order, $plain_text = false ) {
// Getting the email ID global variable
$refNameGlobalsVar = $GLOBALS;
$email_id = $refNameGlobalsVar['email_id_str'];
// If empty email ID we exit
if(empty($email_id)) return;
if ( 'customer_processing_order' == $email_id || 'new_order' == $email_id || 'customer_on_hold_order' == $email_id ) {
$product = wc_get_product( $item->get_product_id() );
echo '<div style="color:#378cbd"><p>' . estimated_shipping_date_message( $product ) . '</p></div>';
}
}
I'm not sure I should insert here the code for the estimated_shipping_date_message() function. It basically echoes a date whether the product is in stock or not.
I'm looking for any pointers on how to tackle this issue. Please tell me if more info is needed here.
Thank you very much! :)