4

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?

7

Your code should work. Simply use:

$item->getQtyOrdered();
5

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());
}
  • 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. – david.soares.MS Jan 16 '17 at 11:50
  • 1
    @david.soares.MS mark an answer as correct (the green check image) – sandip Jan 16 '17 at 12:16
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
  • Thanks nikola99 i'll try that. I'll come back later to tell if it worked :) – david.soares.MS Jan 15 '17 at 17:02
0

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++;
    }

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.