6

I am trying to get all item ids currently added in cart. Here is what I have done so far.

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


public function afterAddProduct($subject, $productInfo, $requestInfo = null)
{
    $productInfo = $this->_cart->getQuote()->getItemsCollection();
    foreach ($productInfo as $item){
       echo $item->getProductId();
    }
}

But it is not returning anything. I have also used $item->getId(); but not avail.

Can anybody tell me what wrong I am doing.

3
  • Have you try below answer solution ? Mar 31, 2017 at 13:45
  • Sorry, but it was my mistake. I found die(); code on top of the file. I removed that and my own code gave id. Thanks for your effort. Apr 1, 2017 at 5:05
  • 1
    You should learn to use xdebug, you’ll never have to type ‘die()’ again May 1, 2018 at 20:37

3 Answers 3

0

Use

$productInfo = $this->_cart->getQuote()->getAllVisibleItems();

instead of

$productInfo = $this->_cart->getQuote()->getItemsCollection();

0
private $checkoutSession;

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

public function getCartItemIds()
{
    $cartItems = $this->checkoutSession->getQuote()->getAllVisibleItems();
    foreach($cartItems as $cartItem) {
        echo $cartItem->getProductId(); //The entity_id of the product            
        echo $cartItem->getId(); //The entity_id of the cart item
    }
}

This should help you understand how to best interact with the cart, assuming you are accessing this in the frontend area.

Obtain the current checkoutSession by injecting a Proxy class for the Checkout\Model\Session class. This prevents the session being loaded during class construction, and defers loading until it is called in the getCartItemIds() method.

The checkoutSession->getQuote() method will return an instance of Magento\Quote\Model\Quote which has the getAllVisibleItems() method. This method returns items that have not been deleted from the cart, and which do not have a parent (configurable options).

Each cart item has a product_id and an entity_id. These two fields are different, as one is the identifier for the quote_item record and one is the entity_id of the catalog_product_entity record it relates to, as shown in the example.

Be sure to run bin/magento setup:di:compile to generate the Proxy class if needed.

0

Get quote by customer id :

public function getQuoteByCustomerId($customerId, $storeId){
    $quote = $this->_quoteFactory->create()->loadByCustomer($customerId);
    $quoteId = $quote->getId();
    if (!$quoteId) {
        $quote = $this->_quoteFactory->create()
            ->setStoreId($storeId)
            ->setIsActive(true)
            ->setIsMultiShipping(false)
            ->save();
        $quoteId = (int) $quote->getId();
        $customer = $this->_customerRepository
            ->getById($customerId);
        $quote->assignCustomer($customer);
        $quote->setCustomer($customer);
        $quote->getBillingAddress();
        $quote->getShippingAddress()->setCollectShippingRates(true);
        $quote->collectTotals()->save();
    }
    return $quote;
}

Get quote for guest :

public function createQuoteForNotLoggedInUser($storeId){
    $quote = $this->_quoteFactory->create()
        ->setStoreId($storeId)
        ->setIsActive(true)
        ->setIsMultiShipping(false)
        ->save();
    $quote->getBillingAddress();
    $quote->getShippingAddress()->setCollectShippingRates(true);
    $quote->collectTotals()->save();
    return $quote;
}

With _quoteFactory define by :

/**
* $_quoteFactory
*
* @var \Magento\Quote\Model\QuoteFactory
*/
protected $_quoteFactory;

You can get quoteId simple by code :

$quoteId = $quote->getId();

Your Answer

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

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