0

I use the following script to update the stock levels of products from a csv. Worked well until recently, where I created a custom observer where I am checking if the product save occurred in the admin product edit page. Now, the script returns the error:

Fatal error: Call to a member function getFullActionName() on null

Why would this be happening when it should be able to do this?

The Script

 <?php
 define('MAGENTO', realpath(dirname(__DIR__)));
 require_once MAGENTO . '/app/Mage.php';

 umask(0);
 Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
 $count = 0;

 $file = fopen(MAGENTO . '/apiscripts/tstt.csv', 'r');
 while (($line = fgetcsv($file)) !== FALSE) { 

 if ($count == 0) {
 foreach ($line as $key=>$value) {
 $cols[$value] = $key;
 } 
 } 

 $count++;

 if ($count == 1) continue;

 #Convert the lines to cols 
 if ($count > 0) { 
 foreach($cols as $col=>$value) {
 unset(${$col});
 ${$col} = $line[$value];
 } 
 }

 // Check if SKU exists
 $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); 

 if ( $product ) {

 $productId = $product->getIdBySku($sku);
 $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
 $stockItemId = $stockItem->getId();
 $stock = array();

 if (!$stockItemId) {
 $stockItem->setData('product_id', $product->getId());
 $stockItem->setData('stock_id', 1); 
 } else {
 $stock = $stockItem->getData();
 }

 foreach($cols as $col=>$value) {
 $stock[$col] = $line[$value];
 } 

 foreach($stock as $field => $value) {
 $stockItem->setData($field, $value?$value:0);
 }


 $stockItem->save();

 unset($stockItem);
 unset($product);
 echo "<br />Stock updated $sku";
 }
 }
 fclose($file);

?>

Snippet of the observer

$actionInstance = Mage::app()->getFrontController()->getAction()->getFullActionName();
    if ($actionInstance == 'adminhtml_catalog_product_save') {  ...}

The full observer code

    public function saveStockItemAfter($observer)
{
    $actionInstance = Mage::app()->getFrontController()->getAction()->getFullActionName();
    if ($actionInstance == 'adminhtml_catalog_product_save') {    
           $stockItem = $observer->getEvent()->getItem();
           $custReason = Mage::app()->getRequest()->getParam('stock_change_reason');

        if (!$stockItem->getStockStatusChangedAutomaticallyFlag() && $stockItem->getOriginalInventoryQty() != $stockItem->getQty()) {
            if (!$message = $stockItem->getSaveMovementMessage()) {
                    if (Mage::getSingleton('api/session')->getSessionId()) {
                        $message = sprintf('Stock saved from Magento API%s', $this->_getStockReason($custReason) ?': '.$this->_getStockReason($custReason):'');
                    } else {
                        $message = sprintf('Manual Save%s', $this->_getStockReason($custReason) ?': '.$this->_getStockReason($custReason):'');
                    }
                }
                $this->insertStockMovement($stockItem, $message);
            }           
    }
}

CSV

enter image description here

  • which event you are using for observer ? – Pawan Jan 1 '19 at 12:53
  • @Pawan cataloginventory_stock_item_save_after – Vaishal Patel Jan 1 '19 at 12:56
  • you can use catalog_product_save_after event and get the product data in observer. – Pawan Jan 1 '19 at 13:24
  • @Pawan interesting, but what would be the benefit of using this over the current event? – Vaishal Patel Jan 1 '19 at 13:33
  • catalog_product_save_after will work when product will save and cataloginventory_stock_item_save_after will fire when inventory will update.So may be earlier one can work in any update to product. – Pawan Jan 1 '19 at 13:48
0

It looks like your observer isn't setup correctly. You should be getting the full action name from the observer object instead of instantiating Mage:app()

Ex.

class Namespace_Module_Model_Observer {
    public function beforeProductSave($observer) {
        $actionInstance = $observer->getEvent()->getControllerAction()->getFullActionName();

        if ($actionInstance == 'adminhtml_catalog_product_save') {
            // Product was saved from admin
            // Add any additional code here
        }
    }
}

Also, make sure you're passing an $observer object as a parameter to your observer method. The name $observer is arbitrary but standard practice.

| improve this answer | |
  • The observer is working well bar the script. Where the error is occurring. Trying to get the action name from the observer event gets Call to a member function getFullActionName() on null even on admin product save. – Vaishal Patel Jan 1 '19 at 13:07
  • I have added my full observer code above – Vaishal Patel Jan 1 '19 at 13:52

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.