Question

 class Productinfo extends \Magento\Framework\View\Element\Template
 {

 protected $checkoutSession;

 protected $quoteFactory;

 protected $collection;        
/**
 * Constructor
 *
 * @param \Magento\Framework\View\Element\Template\Context  $context
 * @param array $data
 * @param \Magento\Checkout\Model\SessionFactory $checkoutSession
 */
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Checkout\Model\SessionFactory $checkoutSession,       
    \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory $collection,
    array $data = []
) {
    $this->checkoutSession = $checkoutSession;
    $this->quoteFactory = $quoteFactory;
    $this->collection = $collection;
    parent::__construct($context, $data);
}

/*
* Get Quote Proudcts
* @return array() 
*/

public function getProducts() {
 $quoteId = $this->checkoutSession->create()->getQuoteId();
 $collection  = $this->collection->create();
 $collection->addFieldToFilter('quote_id',$quoteId);   
 print_r($collection);die;    

}

}

Colleciton is empty . If i use object manager it returns the data. Please let me know where the mistake is

Was it helpful?

Solution

Try below code

.............

use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Api\CartRepositoryInterface;


class CartManagement
{

    protected $quoteFactory;

    protected $quoteRepository;

    public function __construct(
        QuoteFactory $quoteFactory,
        CartRepositoryInterface $quoteRepository
    ) {
        $this->quoteFactory = $quoteFactory;
        $this->quoteRepository = $quoteRepository;
    }

    ............

    //Using Factory Method by quote id
    $quote = $this->quoteFactory->create()->load($quoteId);
    foreach ($quote->getAllItems() as $item) {
        echo $item->getItemId();
    }

    //Using Repository Method by cart id
    $quote = $this->quoteRepository->getActive($cartId);
    foreach ($quote->getAllVisibleItems() as $item)
    {
        echo $item->getSku();
    }

}

OTHER TIPS

Try this code

<?php

namespace Vendorename\ModuleName\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;

class Edit extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
    protected $request;
    protected $cart;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Framework\App\Request\Http $request
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->request = $request;
        $this->cart = $cart;
        parent::__construct($context);
    }

    public function execute()
    {
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $quoteid = 123 // add your quote id here
        // get quote product by items id
        try {
            $quote_data = $this->cart->getQuote()->getItemById($quoteid);
            print_r($quote_data->getData());
            $quote_params = $quote_data->getBuyRequest();
            print_r($quote_params);
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addNoticeMessage($e->getMessage());
        } catch (Exception $e) {
            $this->messageManager->addExceptionMessage($e, __('We can\'t update the item right now.'));
        }
        exit();
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());
        return $resultRedirect;
    }
}

I Hope This Helps You.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top