Skip to content

Instantly share code, notes, and snippets.

@hslaszlo
Created December 8, 2023 17:23
Show Gist options
  • Save hslaszlo/a1ec2395ea2d410d181b3e3e1804058e to your computer and use it in GitHub Desktop.
Save hslaszlo/a1ec2395ea2d410d181b3e3e1804058e to your computer and use it in GitHub Desktop.
Show Availability Stock Status in Woocommerce Email
<?php
add_filter( 'woocommerce_display_item_meta', 'show_stock_status_in_email', 10, 3 );
function show_stock_status_in_email( $html, $item, $args ) {
// gets the product object
$product = $item->get_product();
// gets stock status product
$stock_status = $product->get_stock_status();
// show it after the product name
$html .= '<p style="margin:0;"><strong>(' . $stock_status . ')</strong></p>';
return $html;
}
/* or */
/** Add custom stock message to order emails - 14-02-2022 **/
add_action( 'woocommerce_order_item_meta_end', 'rt_order_item_meta_end', 10, 4 );
function rt_order_item_meta_end( $item_id, $item, $order, $plain_text ) {
$product = $item->get_product();
// if product is on backorder and backorder is allowed (adjust accordingly to your shop setup)
if ( $product->backorders_require_notification() && $product->is_on_backorder( $item['quantity'] ) ) {
echo '<p style="color:#ff5349; font-size:12px;">Not in stock</p>';
}
// else do this
else {
echo '<p style="color:#83b735; font-size:12px;">In stock</p>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment