◦ Comprehensive security
◦ 24/7 support
Magento Guide → Cart → Get All Products
How to get all products from a Magento 2 cart
Need to retrieve every product from a Magento 2 shopping cart? Whether you’re customizing the checkout flow or syncing data to an external system, Magento’s quote object makes it possible. Let’s walk through how to get all cart items—and the key data that comes with them.
Host Magento at full throttle.
Get secure, reliable Magento hosting so you can scale faster.
Why would you need to get all cart items?
Accessing all products in a Magento 2 cart is a common requirement for both frontend customization and backend automation. Here are some scenarios where this is essential:
- Custom checkout modules that display advanced pricing, promotions, or product bundles.
- Analytics and reporting tools that collect SKU, quantity, or price data during checkout events.
- Third-party integrations with CRMs, ERPs, or shipping software that need real-time cart contents.
- Cart reminders and abandoned cart emails that show specific items the customer left behind.
- Dynamic shipping rates or discounts that depend on cart quantity or product type.
If your module or extension needs to understand what’s in the cart, accessing the quote object is the right place to start.
Access the quote object in Magento 2
The quote object stores all information about the current shopping cart. You can access it through the checkout session.
Inject the session dependency
Use dependency injection in your constructor:
protected $checkoutSession;
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession
) {
$this->checkoutSession = $checkoutSession;
}Load the quote
Then retrieve the quote like this:
$quote = $this->checkoutSession->getQuote();This $quote object now gives you full access to all cart data.
Get all cart items from the quote
Magento provides two ways to get cart items:
- $quote->getAllItems() returns every item in the cart, including hidden child products and deleted entries.
- $quote->getAllVisibleItems() gives you only the items that should appear in the cart UI (no deleted or child items).
Example:
foreach ($quote->getAllVisibleItems() as $item) {
$productId = $item->getProductId();
$name = $item->getName();
$qty = $item->getQty();
}You can use this data in templates, APIs, or logic modules.
Extract subtotal, grand total, and addresses
The quote object also gives you access to financial and customer address information.
Subtotals and totals
$subtotal = $quote->getSubtotal();
$grandTotal = $quote->getGrandTotal();Use this for building invoices, payment summaries, or validating discounts.
Billing and shipping addresses
$billingAddress = $quote->getBillingAddress();
$shippingAddress = $quote->getShippingAddress();
$billingName = $billingAddress->getFirstname() . ' ' . $billingAddress->getLastname();
$shippingStreet = $shippingAddress->getStreetLine(1);This is helpful for custom shipping logic or syncing with external systems.
Get quote items by ID or SKU
If you only need certain items from the cart, you can filter based on product ID or SKU:
foreach ($quote->getAllItems() as $item) {
if ($item->getSku() === 'my-sku') {
// do something with this item
}
}Each $item also includes methods like getId(), getPrice(), and getProductType().
Display cart items in a template file
You can show quote items in a .phtml file by exposing the quote from a block class.
Block class method:
public function getQuote() {
return $this->checkoutSession->getQuote();
}Template file example:
$items = $block->getQuote()->getAllVisibleItems();
foreach ($items as $item) {
echo $item->getName() . ' - Qty: ' . $item->getQty();
}Use this to customize the shopping cart or checkout summary page.
Export cart products for integration or reporting
To pass cart data to an external service or report, loop through the items and store the data in an array or JSON string.
Example export array:
$export = [];
foreach ($quote->getAllVisibleItems() as $item) {
$export[] = [
'sku' => $item->getSku(),
'name' => $item->getName(),
'qty' => $item->getQty(),
'price' => $item->getPrice()
];
}
$json = json_encode($export);You can now send $json to an API, save it to a file, or use it in your custom module.
FAQs
Next steps for getting all products from a Magento 2 cart
Accessing quote data is a key part of building Magento 2 modules, from simple cart displays to full checkout customization and external system integration. It’s one of the most flexible and powerful tools in the Magento toolbox.
To continue, try loading and filtering cart data from an observer or API endpoint to fit your unique use case.
Ready to upgrade your Magento experience? Professional hosting improves speeds, security, and reliability for a website and a brand that people find engaging and trustworthy.
Liquid Web offers the raw infrastructure power you need with mission-critical features that keep your store running smoothly. Most importantly, our in-house Magento experts are standing by to help with both hosting and Magento application roadblocks.
Click through below to explore all of our Magento hosting options, or chat with an expert right now to get answers and advice.
Ready to get started?
Get the fastest, most secure Magento hosting on the market
Additional resources
What is Magento Ecommerce? →
A complete beginner’s guide to the Magento Ecommerce platform
Magento shopping cart: How to setup, optimize, and manage →
Let’s walk through everything you need to know about configuring, optimizing, and maintaining your Magento cart the right way.
Best Magento ERP extensions →
Our top 10 compared so you can decide which is best for your business