2

I have tried to display the data from Quote_item table in the database to file .phtml but it shows this error Q

the code in Magento/MyHistory/Block/View.php

<?php
   namespace Magento\History\Block;
   class View extends \Magento\Framework\View\Element\Template
   {
   public function __constructor(
    ....
    /Magento/Checkout/Model/Cart $cart
   ....)
   {
   ...
    $cart = $cart;
   ...
  }
   public function getCartData(){
       $data = $this->cart()->create()->getCollection()- 
         >addFieldToSelect('*');
       return $data;
   }
   }

in Magento/MyHistory/view/frontend/template/index.phtml like this

<?php
     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
     $orders = $objectManager->create('Magento\Checkout\Model\Cart')- 
     >getCollection();
     $orders->addFieldToSelect('*');
?>     

<table class="data table" id="test-data-table">
     <h2 style="color: red;">Your Order Detail</h2>
     <caption class="table-caption">Look at this table</caption>
     <thead>
           <tr>
              <th scope="col" class="col image">Image</th>
              <th scope="col" class="col sku">Sku</th>
              <th scope="col" class="col name">Name</th>
              <th scope="col" class="col qty">Quantity</th>
              <th scope="col" class="col created_at">Create at</th>
              <th scope="col" class="col updated_at">Update at</th>
          </tr>
    </thead>
    <tbody>
          <?php foreach ($orders as $item): ?>
               <tr>
                   <td data-th="Title" class="col sku"><?php echo $item- 
                   >getImage(); ?></td>
                   <td data-th="Title" class="col sku"><?php echo $item- 
                   >getSku(); ?></td>
                   <td data-th="Author" class="col name"><?php echo $item- 
                   >getName(); ?></td>
                   <td data-th="Content" class="col qty"><?php echo $item- 
                   >getQty(); ?></td>
                   <td data-th="Date" class="col created_at"><?php echo 
                   $item->getCreatedAt(); ?></td>
                   <td data-th="Date" class="col updated_at"><?php echo 
                   $item->getUpdatedAt(); ?></td>
               </tr>

               <?php endforeach; ?>
          </tbody>
     </table>

Anyone can help me, please?

4
  • Please add the code here
    – Ravi Soni
    Commented Jun 28, 2019 at 7:19
  • please attached your code Commented Jun 28, 2019 at 7:31
  • It seems like your collection object is blank. Please check that
    – nishu
    Commented Jun 28, 2019 at 7:32
  • I do not understand it clearly. Can you explain it to me? I am new to Magento 2
    – Kite Hany
    Commented Jun 28, 2019 at 7:34

3 Answers 3

1

You can use the below code to get data from Quote Item

Magento\History\Block\View.php

<?php
namespace Magento\History\Block;
class View extends \Magento\Framework\View\Element\Template
{

    protected $cart;

    public function __constructor(
        ...
        \Magento\Checkout\Model\Cart $cart
        ...
    )
    {
        $cart = $cart;
    }

    public function getCartData(){
        $itemsCollection = $cart->getQuote()->getItemsCollection();
        // get array of all items what can be display directly
        $itemsVisible = $cart->getQuote()->getAllVisibleItems();
        // get quote items array
        $items = $cart->getQuote()->getAllItems();
        return $items;
    }
}

Magento/MyHistory/view/frontend/template/index.phtm

<?php $cartData = $block->getCartData(); ?> 

Now here in the Phtml you can get your data with using this $cartData variable.

5
  • It's not working for me @Ravi Soni
    – Kite Hany
    Commented Jun 28, 2019 at 7:43
  • I have updated my answer please check now.
    – Ravi Soni
    Commented Jun 28, 2019 at 7:47
  • Sorry, @Ravi Soni, it does not display any data for me.
    – Kite Hany
    Commented Jun 28, 2019 at 7:55
  • Now what error you get can you please mention here.
    – Ravi Soni
    Commented Jun 28, 2019 at 7:56
  • it does not display any data for me. Although I already echo the data it still displays an empty page
    – Kite Hany
    Commented Jun 28, 2019 at 7:58
1

Try like this :

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orders = $objectManager->get('Magento\Checkout\Model\Cart');

// get quote items collection
$itemsCollection = $orders->getQuote()->getItemsCollection();

// get array of all items what can be display directly
$itemsVisible = $orders->getQuote()->getAllVisibleItems();

// get quote items array
$items = $orders->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 />";            
}
1

You get a fatol error for ->addFieldToSelect('*') you can use addFieldToSelect when you inject collection model in your construct. and also you don't have to use addFieldToSelect you can also get data without it.

app/code/Namespace/Modulename/Block/YourCustomBlock.php

<?php
namespace Namespace\Modulename\Block;

class YourCustomBlock extends \Magento\Framework\View\Element\Template
{ 
    protected $_cart;
 
    protected $_checkoutSession; 
  
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    ) {
        $this->_cart = $cart;
        $this->_checkoutSession = $checkoutSession;
  
        parent::__construct($context, $data);
    }
  
    public function getCart() { 
        return $this->_cart;
    }
  
    public function getCheckoutSession() {
        return $this->_checkoutSession;
    }
}

Your phtml file should be like this

// Get all items in cart
$quote = $block->getCheckoutSession()->getQuote();
$items = $quote->getAllItems();
  
foreach($items as $item) {
    echo 'Product 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 /><br />"; 
}


// Get total items and total quantity in cart
$totalItems = $quote->getItemsCount();
$totalQuantity = $quote->getItemsQty();
 
//Get subtotal and grand total price of cart
$subTotal = $quote->getSubtotal();
$grandTotal = $quote->getGrandTotal();
 
//Get billing and shipping addresses
$billingAddress = $quote->getBillingAddress();
$shippingAddress = $quote->getShippingAddress();

Take reference from Get Shopping Cart Data (Items, Subtotal, Grand Total, Billing & Shipping Address) in Magento 2? and Magento 2: Get all shopping cart items, subtotal, grand total, billing & shipping address

I hope it helps!

3
  • Thank for the answer but its display it displays this Uncaught Error: Call to a member function getQuote() on null in C:\xampp\htdocs\magento2\app\code\Magento\History\view\frontend\templates\index.phtml:3 Stack trace: #0 C:\xampp\htdocs\magento2\lib\internal\Magento\Framework\View\TemplateEngine\Php.php(59): include() #1 C:\xampp\htdocs\magento2\lib\internal\Magento\Framework\View\Element\Template.php(270): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Framework\View\Element\Template), 'C:/xampp/htdocs...', Array) #2
    – Kite Hany
    Commented Jun 28, 2019 at 9:31
  • remove var and generated folder then check Commented Jun 28, 2019 at 9:50
  • check error in var/log file Commented Jun 28, 2019 at 10:03

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.