0

I need to add an iframe for tracking the orders, and the code needs some values like Subtotal, order id, name of every product in the order and also the quantity for each product.

I found a solution for order id and subtotal value.

<?php
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$orderData = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId());
$transaction_id = $orderData->getData()['increment_id'];
$amount = number_format($orderData->getData()['subtotal'],2);
?>

From getData() function you can retrieve informations like, subtotal, customer email, order id, status of the order, store id, shipping description, etc.

But nothing about the products, like name, sku, quantity, description etc.

How can I retrieve this information, the list of all products name and quantity of each product?

1 Answer 1

0

If you have Order object then you can get order items adn other.

  • customer email: $orderData->getCustomerEmail();

  • Order id: $orderData->getId();

  • Store Id: $orderData->getStoreId();

  • shipping description id: $orderData->getShippingDescription();

  • Order status id: $orderData->getStatus();

For order items:

$orderData->getAllItems();

 foreach($orderData->getAllItems() as $item) {
        echo "SKU: " . $item->getSku();
        echo "Product Name: " . $item->getName();
         echo "Qty: " . $item->getQtyOrdered();
       echo "Qty: " . $item->getProductId();
    }

Note:You can any field value of order from this variable $orderData.

Warning: use of object manager is not good idea.

2
  • Thanks for your answer. So how else should I retrieve this information, if this way is not a good idea. The success.php file is overwritten in my theme, and I also have another success.phtml file in the module for the case where the payment is made with a card. Commented Mar 20, 2018 at 10:06
  • I hope,if you will do google then you can rest of data
    – Amit Bera
    Commented Mar 20, 2018 at 10:32

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.