How to get the particular product quantity in orders in Woocommerce

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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;
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x