phpwordpresswoocommercestockemail-notifications

Display the Remaining Stock Quantity for each order item only in WooCommerce new order email notification


I'm having a small issue with a code snippet that is displaying the remaining product stock quantity for each order item in Admin New Order email notification.

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' ) {
            // Add action to display remaining stock quantity on order item meta
            add_action( 'woocommerce_order_item_meta_start', 'display_remaining_stock_quantity', 10, 3 );
        }
    }
    
    // Function to display remaining stock quantity on order item meta
    function display_remaining_stock_quantity(int $item_id, object $item, object $order): void {
        // Only display for line items
        if (!$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. which I dont want. It should ideally be added only to that first New Order Notification that is sent to admins.

I am clearly missing something? Some assistance would be very much appreciated. Thank you!


Solution

  • An alternative way:

    There is an alternative code solution that I have already used before. Try the following:

    // Setting the email ID as a global variable
    add_action('woocommerce_email_before_order_table', 'set_the_email_id_as_a_global_variable', 1, 4);
    function set_the_email_id_as_a_global_variable($order, $sent_to_admin, $plain_text, $email){
        $GLOBALS['email_id_str'] = $email->id;
    }
    
    // Display product remaining stock quantity on order items
    add_action( 'woocommerce_order_item_meta_start', 'display_remaining_stock_quantity', 10, 3 );
    function display_remaining_stock_quantity( $item_id, $item, $order ) {
        // Only for order item "line item" type
        if ( !$item->is_type('line_item') ) {
            return;
        }
        $globalsVar = $GLOBALS; // Pass $GLOBALS variable as reference
    
        // Try to get the email ID from globals reference 
        if( isset($globalsVar['email_id_str']) && $globalsVar['email_id_str'] === 'new_order' ) {
            $product = $item->get_product();
    
            if ( $stock_qty = $product->get_stock_quantity() ) {
                printf('<div>%s: %d</div>', __('Remaining stock', 'woocommerce'), $stock_qty);
            }
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works. It may work on your side, avoiding the issue you describe.


    Some related answers: