Question

I am new to Magento 2 so I do not understand much about select data from the table in Localhost also which file I need to create

I want to display data from Quote_item table

enter image description here

in the .phtml file, and What I need to do in Block and xml file

If anyone can help me step by step, it's very useful for me

Was it helpful?

Solution

You can get data of quote_item table using Magento\Quote\Model\ResourceModel\Quote\Item\Collection collection class like this..

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$quoteItemCollection = $objectManager->create('\Magento\Quote\Model\ResourceModel\Quote\Item\Collection');

foreach ($quoteItemCollection as $quoteItem) 
{
    echo $quoteItem->getName();
    echo $quoteItem->getProductId();
    ......
}

I am not recommend you to use object manager instead inject this collecton class in block class of this phtml file and use it here.

OTHER TIPS

You can get quote item collection by quote id using this below way :

protected $quoteFactory;

public function __construct(
  .....
  \Magento\Quote\Model\QuoteFactory $quoteFactory
  ......
){
   $this->quoteFactory = $quoteFactory;
}

public function yourFunction()
{
    $quote = $this->quoteFactory->create()->load($quoteId);
    $items = $quote->getAllItems();
    foreach ($items as $item) 
    {
        echo $item->getId()."<br>";
        echo $item->getName()."<br>";
        echo $item->getProductId()."<br>";
    }
}

Now, print $items. You'll get data. Hope, it will helpful for you.

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