{"id":1016,"date":"2024-05-25T09:57:16","date_gmt":"2024-05-25T09:57:16","guid":{"rendered":"https:\/\/www.appfinz.com\/blogs\/?p=1016"},"modified":"2024-05-25T09:57:16","modified_gmt":"2024-05-25T09:57:16","slug":"display-the-remaining-stock-quantity-for-each-order-item-only-in-woocommerce-new-order-email-notification","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/display-the-remaining-stock-quantity-for-each-order-item-only-in-woocommerce-new-order-email-notification\/","title":{"rendered":"Display the Remaining Stock Quantity for each order item only in WooCommerce new order email notification"},"content":{"rendered":"\n<p>There is an alternative code solution that you should use: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">\/\/ Setting the email ID as a global variable\nadd_action('woocommerce_email_before_order_table', 'set_the_email_id_as_a_global_variable', 1, 4);\nfunction set_the_email_id_as_a_global_variable($order, $sent_to_admin, $plain_text, $email){\n    $GLOBALS['email_id_str'] = $email-&gt;id;\n}\n\n\/\/ Display product remaining stock quantity on order items\nadd_action( 'woocommerce_order_item_meta_start', 'display_remaining_stock_quantity', 10, 3 );\nfunction display_remaining_stock_quantity( $item_id, $item, $order ) {\n    \/\/ Only for order item &quot;line item&quot; type\n    if ( !$item-&gt;is_type('line_item') ) {\n        return;\n    }\n    $globalsVar = $GLOBALS; \/\/ Pass $GLOBALS variable as reference\n\n    \/\/ Try to get the email ID from globals reference \n    if( isset($globalsVar['email_id_str']) &amp;&amp; $globalsVar['email_id_str'] === 'new_order' ) {\n        $product = $item-&gt;get_product();\n\n        if ( $stock_qty = $product-&gt;get_stock_quantity() ) {\n            printf('&lt;div&gt;%s: %d&lt;\/div&gt;', __('Remaining stock', 'woocommerce'), $stock_qty);\n        }\n    }\n}<\/pre><\/div>\n\n\n\n<p>Code goes in <strong>function.php<\/strong> file of your child theme (or in a plugin). Tested and works. It may work on your side, avoiding the issue you describe.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is an alternative code solution that you should use: Code goes in function.php file<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[145],"tags":[],"class_list":["post-1016","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=1016"}],"version-history":[{"count":1,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1016\/revisions"}],"predecessor-version":[{"id":1017,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1016\/revisions\/1017"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=1016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=1016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=1016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}