0

I try to get qty for a single product which came from o grouped product after a finish my order

In my observer i can take the product name, id,price, attribute id and so on but i can't take the qty

I have the fallowing code in my observer:

   public function execute(\Magento\Framework\Event\Observer $observer)
{
      $order = $observer->getEvent()->getOrder();
      $order_id = $order->getIncrementId();
     foreach($order->getAllItems() as $item){
             $ProdustIds = $item->getProductId();
     // product name
     $proName[] = $item->getName(); 


    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customProduct = $objectManager->get('Magento\Catalog\Model\Product')->load($ProdustIds);

     $productAttributeId = $customProduct->getData('attribute_set_id');

    //get attribute set name for each product
     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $attributeSet = $objectManager->create('Magento\Eav\Api\AttributeSetRepositoryInterface');
    $attributeSetRepository = $attributeSet->get($productAttributeId);
    $attribute_set_name = $attributeSetRepository->getAttributeSetName();
    $attribute_name = strtolower($attribute_set_name);

    //get prodcut price
    $productAttributePrice = $customProduct->getData('price');
    //product qty
    $productAttributeQty = $customProduct->getData('qty');
    $proQty[] = $item->getQty();

I already tried with $productAttributeQty = $customProduct->getData('qty'); and

$proQty[] = $item->getQty()

But is not working.

Does anyone know where is the problem?

1 Answer 1

0

Use below code:

public function execute(\Magento\Framework\Event\Observer $observer)
{
      $order = $observer->getEvent()->getOrder();
      $order_id = $order->getIncrementId();
     foreach($order->getAllItems() as $item){
             $ProdustIds = $item->getProductId();
     // product name
     $proName[] = $item->getName(); 


    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customProduct = $objectManager->get('Magento\Catalog\Model\Product')->load($ProdustIds);

     $productAttributeId = $customProduct->getData('attribute_set_id');

    //get attribute set name for each product
     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $attributeSet = $objectManager->create('Magento\Eav\Api\AttributeSetRepositoryInterface');
    $attributeSetRepository = $attributeSet->get($productAttributeId);
    $attribute_set_name = $attributeSetRepository->getAttributeSetName();
    $attribute_name = strtolower($attribute_set_name);

    //use below to get ordered qty
     echo $item->getQtyOrdered();

    //get prodcut price
    $productAttributePrice = $customProduct->getData('price');
    //product qty
    $productAttributeQty = $customProduct->getData('qty');
    $proQty[] = $item->getQty();
5
  • it gave me the stock qty. how can i get the qty that i buy in the cart for one product? Commented Nov 23, 2016 at 12:56
  • what i try to do, it to take the qty for each product that was add in the cart after the order is ready Commented Nov 23, 2016 at 12:57
  • up[dated answer please check it Commented Nov 23, 2016 at 13:02
  • it donsn't work. i am on magento 2. i think that;s is the problem Commented Nov 23, 2016 at 13:12
  • the solution was $item['qty_ordered']. Thank you very much anyway Commented Nov 23, 2016 at 13:35

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.