0

How can I get the collection of all products in cart currently ?

  • what data need from collection of all products? – Amit Bera Feb 15 '17 at 7:20
3
$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 />";            
}
| improve this answer | |
2

Try it

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

    }

public function getSCurrentUser(){

      $cartQuote=$this->checkoutSession->getQuote();
      $items=$cartQuote->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 />";             
        //get all data here
          //print_r($item->getProduct()->getData());        

        }

}
| improve this answer | |
2

I will reply the question in two parts:

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

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

| improve this answer | |
0

How to get Current cart count in Magento2

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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