Step 1: I want to fetch all products in the cart and Show them with a form individually

Currently, I am fetching data using this code

namespace Softadroit\Prescription\Block;

$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 />";            

And this is my prescription.phtml code, it shows me empty page in the frontend

<h1><?php echo $items = $cart->getQuote()->getAllItems(); ?></h1>

Step 2: How to display that page after placing an order (Before onepage/success)?

Actually I am new to Magento

Any help is really appreciated!

  • Do you want to show current Order Item on that page, right? – Dhiren Vasoya Aug 19 '20 at 10:31
  • Yes I want to show current order items – Arun Kumar Aug 19 '20 at 10:53
  • Did you get the Current Order in your block file? – Dhiren Vasoya Aug 19 '20 at 11:06
  • No I did not get – Arun Kumar Aug 19 '20 at 11:09
  • I update answer, please check. – Dhiren Vasoya Aug 19 '20 at 13:04

You need to get Order in this way :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$sessionObj = $objectManager->get('\Magento\Checkout\Model\Type\Onepage'); 
$session = $sessionObj->getCheckout();
$order = $session->getLastRealOrder();

Now from the $order you need to get there items.

$orderItems = $order->getAllItems();

foreach($orderItems 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 />";            

NOTE: do not use $objectManager, instade of that one include proper class in constructor and then use there object in standard magento way.

  • Can you please elaborate it in steps? – Arun Kumar Aug 19 '20 at 12:50
  • I update answer, please check. – Dhiren Vasoya Aug 19 '20 at 13:03
  • Can you please suggest me how to place this page after checkout (Place Order Button) and before order success page? – Arun Kumar Aug 21 '20 at 6:55

Try below

$obj = \Magento\Framework\App\ObjectManager::getInstance();
$order = $obj->create('\Magento\Checkout\Model\Session')->getLastRealOrder();

$itemsVisible = $order->getAllVisibleItems();
$items = $order->getAllItems();
//other order data
$incrementId = $order->getIncrementId();

You should not use the ObjectManager directly!

  • Now how to display it in phtml file – Arun Kumar Aug 21 '20 at 9:12

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.