magento 1.9 – Custom script causes a member function to be called null


I use the following script to update the inventory of products from a CSV. Until recently, it worked well. There I created a custom observer, checking the Admin product's edit page to see if the product was saved. Now the script returns the error:

Fatal Error: Invoke a member function getFullActionName () with null

Why should that happen when it should be able to?

The script

 setCurrentStore (Mage_Core_Model_App :: ADMIN_STORE_ID);
$ count = 0;

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

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

$ count ++;

if ($ count == 1) next;

#The rows are converted to columns
if ($ count> 0) {
foreach ($ cols as $ col => $ value) {
not set ($ {$ col});
$ {$ col} = $ line[$value];
}
}

// Check if SKU is present
$ product = Mage :: getModel (& # 39; catalog / product & # 39;) -> loadByAttribute (& # 39; sku & # 39 ;, $ sku);

if ($ product) {

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

if (! $ stockItemId) {
$ stockItem-> setData (& # 39; product_id & # 39 ;, $ product-> getId ());
$ stockItem-> setData (& # 39; stock_id & # 39 ;, 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);
not set ($ product);
Echo "
Stock updated $ sku "; } } fclose ($ file); ?>

Detail of the observer

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

The complete code for the observer

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

if (! $ stockItem-> getStockStatusChangedAutomaticallyFlag () && $ stockItem-> getOriginalInventoryQty ()! = $ stockItem-> getQty ()) {
if (! $ message = $ stockItem-> getSaveMovementMessage ()) {
if (Mage :: getSingleton (& ap39; session & # 39;) -> getSessionId ()) {
$ message = sprintf (& # 39 ;, $ this -> _ getStockReason ($ custReason)?): & # 39;: & # 39;. $ this -> _ getStockReason ($ custReason ): & # 39; & # 39;);
} else {
$ message = sprintf (& # 39; Manual Save% s & # 39;, $ this -> _ getStockReason ($ custReason)?): & # 39;: $ this -> _ getStockReason ($ custReason): & # 39; & # 39;);
}
}
$ this-> insertStockMovement ($ stockItem, $ message);
}
}
}

CSV

Enter the image description here