Website Building » WooCommerce » How Do I Get the Product Quantity in a WooCommerce Cart?

How Do I Get the Product Quantity in a WooCommerce Cart?

Last updated on January 18, 2023 @ 5:57 am

If you’re running a WooCommerce store, then you’ve probably wondered how to get the product quantity in a WooCommerce cart. The good news is that it’s actually quite easy to do.

All you need to do is add a few lines of code to your functions.php file.

First, you’ll need to add the following code to your functions.php file:

<?php
add_filter( 'woocommerce_get_item_quantity', 'iconic_get_item_quantity', 10, 3);
function iconic_get_item_quantity( $product_quantity, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
$product_quantity = sprintf( '%s <div class="qty">%s</div>', $product->get_title(), $product_quantity );
return $product_quantity;
}
?>

This code will add a quantity field next to the product name in your cart.

If you want to style the quantity field, you can add the following CSS to your stylesheet:

qty {
float: 
right;
}
Morgan Bash

Morgan Bash

Technology enthusiast and Co-Founder of Women Coders SF.