5/5

WooCommerce Profit Reports
Like You've Never Seen Before

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.

Related Post

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sam
Sam
3 months ago

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

Recent Posts