WooCommerce Get Products In Cart

Alpha Insights

Boost Your ROAS.

Connecting WooCommerce Orders To Your Ad Campaigns Is Just The Beginning...

WooCommerce Cost Of Goods Order Profit Reports

How To Get All Items In A WooCommerce Cart Using PHP

It’s pretty easy to grab all product data from the WooCommerce cart.

For simplicity, we will only really deal with product data in this tutorial. 

You can obviously grab details about the user’s cart, but the purpose of this is to deal with product data.

				
					// Get full cart object
$items = WC()->cart->get_cart();

// Loop through cart items
foreach( $items as $item => $values ) {

    // Load product object
    $product = wc_get_product( $values['data']->get_id() );

    // Access product data using product object
    $product_title = $product->get_title();

    // Get product qty in cart just in case
    $quantity_in_cart = $values['quantity'];

    // Do what you need to do...

} 
				
			

The above code block is pretty much all you need, it’s really simple.

Once you have the $product object you can access any product data that you could imagine.

If you’re not sure how or what you can access through the product object, you can read our how to guide on getting all product info in woocommerce.

Subscribe
Notify of
guest
6 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Marco Floriano
Marco Floriano
1 year ago

Thanks, clear and straight peace of code on how to get the items from the cart. I needed that in order to get the cart products Ids, using product->get_id() method.

terbitbintang189
terbitbintang189
1 year ago

hello guys How are you

Sam
Sam
2 years ago

Hi this code only gets a one item from the cart. How can I get all items?

Alpha Insights

Alpha Insights

The World's Most Advanced WooCommerce Reporting Plugin.

WooCommerce Cost Of Goods Order Profit Reports