I’m having a small issue with my snippet for displaying the remaining stock for admin new order emails.
This is my code:
// Add action to display content on specific email
add_action( 'woocommerce_email_before_order_table', 'add_stock_to_admin_email', 20, 4 );
// Function to display content on specific email
function add_stock_to_admin_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'new_order' && $sent_to_admin ) {
// Add action to display remaining stock quantity on order item meta
add_action( 'woocommerce_order_item_meta_start', 'display_remaining_stock_quantity', 10, 3 );
}
}
// Display remaining stock quantity on order item meta
function display_remaining_stock_quantity(int $item_id, object $item, object $order): void {
// On email notifications for line items
if (is_wc_endpoint_url() ||!$item->is_type('line_item')) {
return;
}
$product = $item->get_product();
// Display remaining stock quantity
$stock_quantity = $product->get_stock_quantity();
if ($stock_quantity > 0) {
printf('<div>%s: %d</div>', __('Remaining stock', 'woocommerce'), $stock_quantity);
}
}
This works fine to add the remaining stock line under each product in the order product table. However it is also adding the remaining stock to customer emails when paid by Bank Deposit/ Order on hold emails etc. I am clearly missing something? Some assistance would be very much appreciated. Thank you!