0

I need to observe API's changes on stockItems from other sources coming to my store, although the below code is working on my local server, the old stock qty is always 0 on the production server.

Expected result (working on local server): | qtyBefore | qtyNow | stockVariation | --- | --- | --- | | 100 | 90 | -10 |

Result (in production server): | qtyBefore | qtyNow | stockVariation | --- | --- | --- | | 0 | 90 | 90 |

I tried several methods of getting the stock qty on save_before event but I only got the new qty or 0. I'm running the same version of php and nginx with the same config on both. I don't know if this can cause this error, but I cloned my local magento instance to the server and deleted all products before creating them again.

My observer methods:

 protected function beforeSave($observerData, $event_name)
    {
        $objManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objManager->create('Magento\Catalog\Model\Product')
            ->load($productId = $observerData->getProductId());
        $stockItem = $this
            ->stockItemRepository
            ->get($productId);

        // Works on local server but on production server it's always 0
        $qtyBefore = $stockItem->getQty();


        $sku = $product->getSku();
        // Saving old stock in registry to calculate the stock' variation in save_after event
        $this
            ->registry
            ->register($productId, array(
                'qty' => $qtyAntes,
                'sku' => $sku
        ));
        return;
    }

    protected function afterSave($observerData, $event_name)
    {
        $productId = $observerData->getProductId();
        // Retrieving old stock qty saved on registry
        $registryAux = $this
            ->registry
            ->registry($productId);
        $sku = $registryAux['sku'];
        $qtyBefore = $registryAux['qty'];
        $qtyNow = $observerData->getQty();
        $is_in_stock = $observerData->getIsInStock();
        $stockVariation = $qtyNow - $qtyBefore;

        return $stockVariation;

    }

    public function execute(Observer $observer)
    {
        $event_name = $observer->getData('event')
            ->getName();
        try
        {
                switch ($event_name)
                {
                    case 'cataloginventory_stock_item_save_before':
                        $this->beforeSave($observer->getEvent()
                            ->getItem() , $event_name);
                    break;
                    case 'cataloginventory_stock_item_save_after':
                        $stockVariation = $this->afterSave($observer->getEvent()->getItem() , $event_name);
                        $this
                            ->logger
                            ->debug('API_CALL_OBSERVER_STOCKVARIATION: ' . $stockVariation);
                    break;
                }
        }
        catch(Exception $e)
        {
            $this
                ->logger
                ->debug('API_CALL_OBSERVER_ERROR: ' . $e->getMessage());
        }
    }

Any help would be apreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.