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.

5 Answers 5


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 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.

  • using Cart model and method get AllItems() or getAllVisibleItems() will return all items in all carts of anybody visiting the store from all active quotes model? Or just one from my cart (I bet for this I'd rather use checkout session object and get qutoe object from it)? Difference is important, because I wanted to use checkout session in block class, but actually qutoe is empty when get inside block. Commented Aug 29, 2016 at 20:52
  • It's always only the cart of the current session. Commented Aug 29, 2016 at 21:08
  • And how to get the category of goods? Commented Mar 22, 2019 at 11:34
  • How can we do this for custom rest API, It returns empty but I have items on my Cart
    – fernandus
    Commented Jun 27, 2020 at 18:09

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)
  • 4
    I believe the proper way is to use the checkout session now Magento\Checkout\Model\Session::getQuote(). Commented Sep 15, 2016 at 8:01
$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 />";
  • And how to get the category of goods? Commented Mar 22, 2019 at 11:34
  • Using $objectManager directly is not a good approach to follow.
    – TheFrakes
    Commented Sep 12, 2023 at 9:21

Like @Frank says


was deprecated in version 100.1.0

Another option is to use a Proxy and benefit from the performance optimization

Here is how:

public function __construct(Magento\Checkout\Model\Session\Proxy $cartSession)
    $this->cartSession = $cartSession;

then retrieve the items:

  • I'm getting the right results while on the checkout page. However on the home pages and product pages im always getting an empty list Commented Mar 14 at 13:46

This code is functional for Magento 2.4.3

public function __construct(
    \Magento\Checkout\Model\Session $session
) {
     $this->checkoutSession = $session;

public function yourFunction()
    $items = $this->checkoutSession->getQuote()->getAllVisibleItems();
    $cartItems = [];
    foreach($items as $item) {
      $cartItems[] = [
       'name' => $item->getName(),
       'sku' => $item->getSku(),
   return $cartItems;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.