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
It goes against some things ive read but this code is working for me
Welcome to WPSE.
$item
is an instance ofWC_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.