Magento 2 - How to get all?

At checkout page (chekout/cart) I want to edit the checkout button url destination based on cart items (link.phtml).

How can I get all items in cart? I want to do this without using the API. Thanks.


The use of Magento\Checkout\Model\Cart is deprecated.

You should use the session to get the quote and your cart items.

use Magento\Checkout\Model\Session;

public function __construct(
        Session $session
    ) {
        $this->_session = $session;

public function yourFunction()
    $items = $this->_session->getQuote()->getAllVisibleItems();

I'm sure that Magento will soon add some kind of repository for session.

As far as I can tell you can't really just get the current quote using using either Magento\Quote\Api\CartRepositoryInterface or Magento\Quote\Api\CartManagementInterface however, if you have the customer ID you can use either of those two to load the quote (which I imagine is the way Magento want you to do it) and from there get quote / cart items.

CartManagementInterface getCartForCustomer($customerId)
CartRepositoryInterface getForCustomer($customerId)

I'll answer the question in two parts:

I. Where should you change the checkout URL?

The destination in link.phtml is $block->getCheckoutUrl(). You should not change the template to change functionality, but change the block Magento\Checkout\Block\Onepage\Link instead. To do so create a plugin for it with a method afterGetCheckoutUrl() to change the return value.

II. How to get all items in the cart?

As of magento-2.0 the service contracts in Magento_Checkout and Magento_Quote do not provide the necessary methods yet, so we use the cart model. In your plugin inject it like this:

public function __construct(\Magento\Checkout\Model\Cart $cart)
    $this->cart = $cart;

Now that you have access to the cart, you can retrieve the items in several ways, which is basically the same as in Magento 1:

  • $this->cart->getQuote()->getItemsCollection()

    Returns a quote item collection with all items associated to the current quote.

  • $this->cart->getItems()

    This is a shortcut for the method above, but if there is no quote it returns an empty array, so you cannot rely on getting a collection instance.

  • $this->cart->getQuote()->getAllItems()

    Loads the item collection, then returns an array of all items which are not marked as deleted (i.e. have been removed in the current request).

  • $this->cart->getQuote()->getAllVisibleItems()

    Loads the item collection, then returns an array of all items which are not marked as deleted AND do not have a parent (i.e. you get items for bundled and configurable products but not their associated children). Each array item corresponds to a displayed row in the cart page.

Choose what fits your needs best. In most cases the last method is what you need.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 

// retrieve quote items collection
$itemsCollection = $cart->getQuote()->getItemsCollection();

// get array of all items what can be display directly
$itemsVisible = $cart->getQuote()->getAllVisibleItems();

// retrieve quote items array
 $items = $cart->getQuote()->getAllItems();

foreach($items as $item) {
     echo 'ID: '.$item->getProductId().'<br />';
      echo 'Name: '.$item->getName().'<br />';
       echo 'Sku: '.$item->getSku().'<br />';
       echo 'Quantity: '.$item->getQty().'<br />';
      echo 'Price: '.$item->getPrice().'<br />';
    echo "<br />";            

Similar questions

Programatically deleting items from cart leaves 'ghost' items;
I am using the observer checkout_cart_save_after to programatically delete some cart items. Whilst this does delete the cart items, it still shows the checkout page as an empty table like so: But if I delete all the items manually on the frontend, I get a much nicer page after I delete the last item: What am I missing?
Empty the Cart items but keep the quote and quote items
I have added a button on cart page. Clicking on the button the cart items should get deleted but the quote should not be erased.
How to get all items with custom options in magento current cart?
Get cart visible products with custom options. I have done with out custom options. How can get custom options from this
Magento 2: Get all shopping cart items, subtotal, grand total, billing & shipping address
I want to Get all shopping cart items, subtotal, grand total, billing & shipping address in one step checkout Magento 2 in storefront (Home Page) . according to the this link : I added new step in checkout page according to t...
How to get all currently?
I want to get a collection of all the items in cart currently. How can I do this ? Any suggestions will be appreciated.
Get all updated cart items data with options in AJAX response
My Cart is looking like this with popup window: I am updating multiple configurable items in cart with custom options at a time by AJAX Call. So i am using updateItemOptionsAction() function. But I am not able to get all items data back to AJAX response. I am getting just 1st itemPrice and rowTotal. For remaining items itemPrice and rowTotal are se...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.