Question

I am working on my WooCommerce project and I am sending mail to the customer and in that I am sending the particular product quantity. So, I am not able to get that particular product quantity from the order.

This is code:

$order = new WC_Order( $order_id );

$item_quantity=0;
$targeted_id = 14988;
foreach ( $order->get_items() as $item_id => $item ) {
$item_quantity += $item->get_quantity();
}

I want to get the '14988' product quantity from the orders. Any help is much appreciated.

Was it helpful?

Solution

In the foreach you need to check each item for its product id and compare it to the targeted id

$order = new WC_Order($order_id);

$item_quantity = 0;
$targeted_id = 14988;

foreach ($order->get_items() as $item_id => $item) {
    // check if current item (product) id is equal to targeted id
    if ($item->get_product_id() == $targeted_id) {
        $item_quantity += $item->get_quantity();
        // once the correct cart item is found we no longer need to loop all other products so we break out of the loop
        break;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top