skip to Main Content

Is it possible to show the stock status as "In Stock" "Pre-Order" in new order mails?

These codes help me show the SKU in Mail, but I could not create the stock status by changing it.

/**
 * Adds SKUs and product images to WooCommerce order emails
 */
function sww_add_sku_to_wc_emails( $args ) {
  
    $args['show_sku'] = true;
    return $args;
}
add_filter( 'woocommerce_email_order_items_args', 'sww_add_sku_to_wc_emails' );

2

Answers


  1. You can show the stock status of the products via the woocommerce_display_item_meta hook, for more information: https://woocommerce.github.io/code-reference/files/woocommerce-includes-wc-template-functions.html#source-view.3290

    Here is the code for your case:

    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;
    }
    

    The result will be the following:

    enter image description here

    The code has been tested and works. Add it in your theme’s functions.php file.

    Login or Signup to reply.
  2. If you want to add custom message instead of the standard "outofstock" and "instock" status, you can use:

    /** 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>';
      }
    }
    

    Code should be added to function.php of your active theme. At best in the function.php of your child theme.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search