1

I tried my code below to get Qty from observer when a page loaded :

Observer :

public function execute(\Magento\Framework\Event\Observer $observer) {
    $collections = $observer->getEvent()->getCollection();
    foreach( $collections as $collection ) {
        var_dump($collection->getQty());
    }
}

events.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
  <event name="catalog_product_collection_load_after">
    <observer name="loadupdates" instance="Test\Show\Observer\CustomPrice" />
  </event>
</config>

But, i got all NULL for getQty of products. So my question, how can i get the Qty of products, and how to update the Qty from the observer?

2 Answers 2

0

If you want to get Inventory of the product, you should inject \Magento\CatalogInventory\Api\StockStateInterface first. Then using getStockQty to get the qty of an product. So that in your observer:

public function __construct(
    \Magento\CatalogInventory\Api\StockStateInterface $stockItem
)
{
    $this->stockItem = $stockItem;
}

public function execute(\Magento\Framework\Event\Observer $observer) {
    $collections = $observer->getEvent()->getCollection();
    foreach( $collections as $collection ) {
        var_dump($this->stockItem->getStockQty($collection->getId()));
    }
}

Ref: How to get stock quantity of each product in Magento 2

1
  • omg i forget to add the injection, thank you for your answer :) Commented Sep 11, 2017 at 3:38
0

If you want to $productobj after saving product from backend side so you can easily use catalog_product_save_after event.

I am assuming that you already know how to create a module in M2.

Right now you have to need develop new module for M2

Then Create this events.xml file in below path

app\code\YOUR_NAMESPACE\YOURMODULE\etc\adminhtml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="test_name" instance="YOUR_NAMESPACE\YOUR_MODULENAME\Observer\Productsaveafter" />
    </event>
</config>

And Create your observer file Productsaveafter.php in below path

app\code\YOUR_NAMESPACE\YOURMODULE\Observer\

<?php

namespace YOURNAMESPACE\YOURMODULENAME\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        $id = $product->getId(); //Get Product Id

        //Get Quantity
        $stockItem = $product->getExtensionAttributes()->getStockItem();
        $stockData = $stockItem->getQty();
        // Get new Qty
        $_vendor_qty = $product->getVendorQty();
        $_on_hand_qty = $product->getOnHandQty();
        $totalQty = $_vendor_qty+$_on_hand_qty; //Add New Qty


        $stockItem->setQty($totalQty); //Set New Qty to Main Qty
        $stockItem->save();

    }   
}

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.