There is an issue when including "stock status" of the purchased items on the client email.
If the client orders the last unit of an item, even though when he ordered it was clearly instock (available for immediate shipping), then the stock status turns to outofstock/onbackorder and the email that is sent to the client (which I assume is sent/generated a few seconds after the stock value is updated from the clients own order) shows this status updated to outofstock/onbackorder, so after completing the purchase now the client thinks the product is out of stock when in fact it was not.
I'm using this code hooked onto my emails:
$product = $item->get_product();
$stockstatus = get_post_meta( $product->get_id(), '_stock_status', true );
if ($stockstatus == 'instock') { 'Available for immediate shipping'; }
elseif ($stockstatus == 'onbackorder') { 'On Preorder - slow shipping';}
We use Stock status to define shipping time of our products, it's simple, if it's in stock = immediate shipping, if it's outofstock/onbackorder = Preorder ( slow shipping )
Clients can view this information individually for each product on the product page and on the cart, however to make it as clear as possible and also keep a record of the Stock Status of when the order was made (so we know and can show to a client if a product was or not in stock when ordered) we also send this information to the client email. The issue being it displays the stock status at the time the email is sent and not at the time of the order (So if it was the last unit and the product turns to out of stock, then clients get the wrong message)
What would be the correct way to go about this situation and instead of displaying the current stock status, displaying the correct stock status at the time that the order was placed, so that the customers get the correct information?
Thank you in advance for the attention and advice
Edit: Here you can find the full code on how I apply this upper snippet to my emails, I apologize I did not share all the code above (the part for editing custom woocommerce email), my intention was to simplify as I felt it would be unrelated/filler/distracting from the main point as there are a few posts already covering how to customize woocommerce emails. These are I believe the main posts that cover this: Credits @Loictheaztec & @7uc1f3r Customize order item meta only for WooCommerce admin email notifications
Display product ACF fields on a specific WooCommerce email
Here is the full code I am using :
// Setting the "sent_to_admin" as a global variable
function email_order_id_as_a_global($order, $sent_to_admin, $plain_text, $email) {
$GLOBALS['email_data'] = array(
'sent_to_admin' => $sent_to_admin, // <== HERE we set "$sent_to_admin" value
'email_id' => $email->id, // The email ID (to target specific email notification)
);
}
add_action('woocommerce_email_before_order_table', 'email_order_id_as_a_global', 1, 4);
function custom_order_item_name( $item_name, $item ) {
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
// Getting the custom 'email_data' global variable
$refNameGlobalsVar = $GLOBALS;
$email_data = $refNameGlobalsVar['email_data'];
// Only for new order
if( is_array( $email_data ) && $email_data['email_id'] == 'new_order' ) {
// Get the WC_Product object (from order item)
$product = $item->get_product();
$product = $item->get_product();
$stockstatus = get_post_meta( $product->get_id(), '_stock_status', true );
if ($stockstatus == 'instock') { 'Available for immediate shipping'; }
elseif ($stockstatus == 'onbackorder') { 'On Preorder - slow shipping';}
}
}
}
return $item_name;
}
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 ); ```