how to get particular product quantity from the cart page in the woocommerce
Asked Answered
S

7

13

With this code:

foreach ( WC()->cart->get_cart() as $cart_item ) {  
   $quantity =  $cart_item['quantity'];
   echo $quantity;
}

I can get the quantity of all the products added in cart but I need it for the particular product.

Swindle answered 19/6, 2017 at 13:0 Comment(1)
just check if statement if($cart_item['product_id'] === 'your-id') {Hydrocele
S
12

You can loop through cart items to get the quantity for a specific product id as follows:

// Set here your product ID (or variation ID)
$targeted_id = 24;

// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) { 
    if( in_array( $targeted_id, array($cart_item['product_id'], $cart_item['variation_id']) )){
        $quantity =  $cart_item['quantity'];
        break; // stop the loop if product is found
    }
}
// Displaying the quantity if targeted product is in cart
if( isset( $quantity ) && $quantity > 0 ) {
    printf( "<p>" . __("Quantity for Product ID %d is: %d") . "</p>", $targeted_id, $quantity );
}

Or you can also use the WC_Cart method get_cart_item_quantities() as follow:

// Set here your product ID (or variation ID)
$targeted_id = 24;

// Get quantities for each item in cart (array of product id / quantity pairs)
$quantities = WC()->cart->get_cart_item_quantities(); 

// Displaying the quantity if targeted product is in cart
if( isset($quantities[$targeted_id]) && $quantities[$targeted_id] > 0 ) {
    printf( "<p>" . __("Quantity for Product ID %d is: %d") . "</p>", $targeted_id, $quantities[$targeted_id] );
}

Note: This last way doesn't allow to target the parent variable product.

Sacculus answered 21/6, 2017 at 3:53 Comment(0)
H
2
global $woocommerce;
    $items = $woocommerce->cart->get_cart();

    foreach($items as $item => $values) { 
        $_product = $values['data']->post; 
        echo "<b>".$_product->post_title.'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
        $price = get_post_meta($values['product_id'] , '_price', true);
        echo "  Price: ".$price."<br>";
    } 
Hanley answered 19/6, 2017 at 13:37 Comment(0)
D
2
$cart = WC()->cart->get_cart();
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
if( WC()->cart->find_product_in_cart( $product_cart_id )) {
    echo($cart[$product_cart_id]['quantity']);
}
Dallon answered 4/5, 2020 at 18:28 Comment(1)
Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.Trin
C
0

Try this :

<?php
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();

        foreach($items as $item => $values) { 
            $_product = $values['data']->post; 
            echo "<b>".$_product->post_title.'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
            $price = get_post_meta($values['product_id'] , '_price', true);
            echo "  Price: ".$price."<br>";
        } 
?>
Creep answered 19/6, 2017 at 13:18 Comment(1)
through this i will get all the XYZ products i need only Y products quantity ...Swindle
D
0

1) Make a child theme

2) Create a file named functions.php with this code :

<?php
if( !function_exists('in_cart_product_quantity') ) {
    function in_cart_product_quantity( $atts ) {

        $atts = shortcode_atts(array('id' => ''), $atts, 'product_qty'); // shortcode attributes
        if( empty($atts['id'])) return; // check id not null

        if ( WC()->cart ) { // check cart exists
            $qty = 0; // init qty
            foreach (WC()->cart->get_cart() as $cart_item) {
                if($cart_item['product_id'] == $atts['id']) {
                    $qty =  $cart_item['quantity'];
                    break; // stop the loop if product is found
                }
            }
            return $qty;
        }
        return;
    }
    add_shortcode( 'product_qty', 'in_cart_product_quantity' );
}

3) Use the shortcode :

[product_qty id="xxx"]

(Change xxx with id of the product you want to know the quantity in cart)

Thanks to LoicTheAztec (Same thread)

Davide answered 7/5, 2020 at 18:24 Comment(0)
R
0
WC_Cart::get_cart_item_quantities() – Get cart items quantities
  1. Via $product_id
// Your product ID
$product_id = 30;

// Get cart items quantities
$cart_item_quantities = WC()->cart->get_cart_item_quantities();

// Product quantity in cart - All PHP versions
$product_qty_in_cart = isset( $cart_item_quantities[ $product_id ] ) ? $cart_item_quantities[ $product_id ] : null;

// Product quantity in cart - Same as the previous one with PHP 7
$product_qty_in_cart = $cart_item_quantities[ $product_id ] ?? null;

// Result
echo $product_qty_in_cart;
  1. Via $product
// Product
global $product;

// Get cart items quantities
$cart_item_quantities = WC()->cart->get_cart_item_quantities();

// Product quantity in cart 
$product_qty_in_cart = $cart_item_quantities[ $product->get_stock_managed_by_id() ];

// Result
echo $product_qty_in_cart;
Reprovable answered 11/11, 2020 at 19:56 Comment(0)
P
0

You can easily send product ID in the following function and it can return 0 if product doesn't exist in the cart and quantity if product does exist in the cart.

if ( ! function_exists( 'wc_wg_get_cart_qty_product_id' ) ) :
    function wc_wg_get_cart_qty_product_id( $product_id ) {
        if ( empty( $product_id ) ) {
            return 0;
        }
        if ( ! class_exists( 'WooCommerce' ) ) {
            return 0;
        }
        $quantities = WC()->cart->get_cart_item_quantities(); 

        if ( isset( $quantities[$product_id] ) && $quantities[ $product_id ] > 0 ) {
            return $quantities[ $product_id ];
        } else {
            return 0;
        }
    }
endif;

To call the function just pass the product id into the function and it will return product quantity from cart if product is in cart.

$quantityinCart = wc_wg_get_cart_qty_product_id( $loop->post->ID );
Pettish answered 16/7, 2023 at 0:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.