0

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

<?php
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!

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

2 Answers 2

1

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.

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

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
$orderId=$order->getEntityId();
$incrementId = $order->getIncrementId();

You should not use the ObjectManager directly!

1
  • Now how to display it in phtml file Commented Aug 21, 2020 at 9:12

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.