skip to Main Content

I need help applying the following filter to woocommerce admin new order emails only. I’ve tried a few things but cant get it working.

**function filter_woocommerce_email_order_item_quantity( $qty_display, $item ) {
$product = wc_get_product( $item['product_id'] );
$product_id = $product->get_id();

if ( $product_id == 6960 ) {
    $qty_display = $qty_display * 2;
}

return $qty_display; 
}; 
add_filter( 'woocommerce_email_order_item_quantity', 'filter_woocommerce_email_order_item_quantity', 10, 2 );

2

Answers


  1. Chosen as BEST ANSWER

    It goes against some things ive read but this code is working for me

    add_action( 'woocommerce_email_order_details', 'add_order_instruction_email', 11, 4 );
    
    function add_order_instruction_email( $order, $sent_to_admin, $plain_text, $email ) {
    
       remove_filter( 'woocommerce_email_order_item_quantity', 'filter_woocommerce_email_order_item_quantity', 10, 2 );
    
      }; 
    
    function filter_woocommerce_email_order_item_quantity( $qty_display, $item ) {
        $product = wc_get_product( $item['product_id'] );
        $product_id = $product->get_id();
    
    if ( $product_id == 6960 ) {
        $qty_display = $qty_display * 2;
    }
        if ( $product_id == 6912 ) {
        $qty_display = $qty_display * 2;
    }
        if ( $product_id == 6861 ) {
        $qty_display = $qty_display * 2;
    }
    
    return $qty_display; 
    }; 
    add_filter( 'woocommerce_email_order_item_quantity', 'filter_woocommerce_email_order_item_quantity', 10, 2 );
    

  2. Welcome to WPSE.
    $item is an instance of WC_Order_Item and not an array, so it’s not possible get the product_id from the order item object like that ($item['product_id']).
    I suggest using WooCommerce standard methods as below.

    Update:

    woocommerce_email_order_item_quantity action filter does not provide an argument to know if it’s an admin or customer email. To do so, we need to do a trick.

    I would hook a function to woocommerce_email_order_details action to find out whether if the current email is to be sent to an admin and save it in a constant to modify the quantity later based on it’s value. Woocommerce hooks it’s function with the priority of 10 to this hook, so we need a lower priority.

    add_action( 'woocommerce_email_order_details', function( $order, $sent_to_admin )
    {
        if ( $sent_to_admin && ! defined('TST_ADMIN_EMAIL') ) {
            define( 'TST_ADMIN_EMAIL', true );
        }
    }, 9, 2 );
    
    add_filter( 'woocommerce_email_order_item_quantity', 'tst_filter_woocommerce_email_order_item_quantity', 10, 2 );
    function tst_filter_woocommerce_email_order_item_quantity( $qty_display, $item )
    {
        if (
            defined('TST_ADMIN_EMAIL')
            && true === TST_ADMIN_EMAIL
            && 'line_item' === $item->get_type()
        ) {
            $product = $item->get_product();
            $product_id = $product->get_id();
    
            if ( $product_id == 6960 ) {
                $qty_display = $qty_display * 2;
            }
        }
    
        return $qty_display; 
    }; 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search