5

I'm trying to build a custom payment module for magento 2 and i have a function in my payment module that is called on the success page. The problem is that i can't get the items quantity. I can get the name, price and sku but not the quantity. Below a bit of the code:

    $order = $this->getOrder($orderId);
    $items = $order->getAllItems();

    $i = 1;
    foreach($items as $item) {
        $product = $item->getProduct();
        $item = "<item id='".$i."'>";
        $item.= "<name>".$product->getName()."</name>";
        $item.= "<infos>".$product->getName()."</infos>";
        $item.= "<amount>".$product->getPrice()."</amount>"; 
        $item.= "<categoryId>".$category_id."</categoryId>"; 
        $item.= "<quantity>".intval($item->getQtyOrdered())."</quantity>"; 
        $item.= "<reference>".$product->getSku()."</reference>"; 
        $item.= "</item>";
        $xmlItems[] = $item;
        $i++;
    }

When using $item->getQtyOrdered(). i get the following error:

Fatal error: Call to a member function getQtyOrdered() on string on line 139.

And when using $product->getQtyOrdered() the result is null.

Now the interesting part is that i use PhpED to debug the code and when i add to the watch list this $item->getQtyOrdered() the result is 1.0000.

What am i missing?

6 Answers 6

9

Your code should work. Simply use:

$item->getQtyOrdered();
8

Use this code to get items from quantity according to order id.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId(000000005);
$orderItems = $order->getAllItems();
foreach ($orderItems as $item) {
  echo $itemQty = $item->getQtyOrdered();
} 
4

Order Line items along with Quantity information. It will print all the data of particular item.

$_orderAllVisibleItems = $_order->getAllVisibleItems();
foreach($_orderAllVisibleItems as $_item ) {
    print_r($_item->getData());
}
3
  • nikola99, thanks for you explanation on your answer. Same, your answer solves my issue. That $_item->getData() returned the data i wanted with quantity in it, thanks. Jan 16, 2017 at 11:50
  • 1
    @david.soares.MS mark an answer as correct (the green check image)
    – sandip
    Jan 16, 2017 at 12:16
  • getData always only take the "maintable data out" if you need more, then you need to use getSku, getOrderedQty and soe, but its a nice quick way to take main data out fast. Aug 15, 2021 at 7:44
2

As Khoa TruongDinh correctly pointed out it should work.

It looks like you're trying to do something similar to Magento's Google Analytics implementation where the purchase details are displayed on the success page vendor/magento/module-google-analytics/Block/Ga.php:115

Keep in mind a few things:

  1. There could be multiple orders on the success page, if multi-address checkout is used.
  2. You probably do not want to use \Magento\Sales\Model\Order::getAllItems since that will return the composite (grouped, configurable, bundle) in addition to the actual purchased products. you probably want to use \Magento\Sales\Model\Order::getAllVisibleItems
1
  • Thanks nikola99 i'll try that. I'll come back later to tell if it worked :) Jan 15, 2017 at 17:02
1

Just use another variable name for preparing your xml string as $item gets overridden.

It should be like this:

foreach($items as $item) {
        $product = $item->getProduct();
        $itemString = "<item id='".$i."'>";
        $itemString.= "<name>".$product->getName()."</name>";
        $itemString.= "<infos>".$product->getName()."</infos>";
        $itemString.= "<amount>".$product->getPrice()."</amount>"; 
        $itemString.= "<categoryId>".$category_id."</categoryId>"; 
        $itemString.= "<quantity>".intval($item->getQtyOrdered())."</quantity>"; 
        $itemString.= "<reference>".$product->getSku()."</reference>"; 
        $itemString.= "</item>";
        $xmlItems[] = $itemString;
        $i++;
    }
0

If you look at the Magento code itself, in app/code/Magento/Shipping/Model/Shipping.php there's a pretty good example of how Magento themselves accesses data from the return value of order->getAllItems.

In there you can find code showing what they use and also how they deal with special products.

https://github.com/magento/magento2/blob/2.4/app/code/Magento/Shipping/Model/Shipping.php

I suggest you look at the code starting at line 378 for what you need in the event Adobe updates the source code, but I'm cutting a piece out for you.

$qty = $item->getQty();

if ($item->getParentItem()) {
    if (!$item->getParentItem()->getProduct()->getShipmentType()) {
        continue;
    }
    $qty = $item->getIsQtyDecimal()
          ? $item->getParentItem()->getQty()
          : $item->getParentItem()->getQty() * $item->getQty();
}

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.