Skip to content

Instantly share code, notes, and snippets.

@shebin512
Last active July 5, 2020 11:29
Show Gist options
  • Save shebin512/3b0858f97831a8107aeb to your computer and use it in GitHub Desktop.
Save shebin512/3b0858f97831a8107aeb to your computer and use it in GitHub Desktop.
Magento get current full Action controller name in Observer
<?php
/*
* Get full Action controller name in Observer
*/
$request = Mage::app()->getRequest();
$module = $request->getControllerModule();
$module_controller = $request->getControllerName();
$module_controller_action = $request->getActionName();
$fullActionName = $module."_".$module_controller."_".$module_controller_action;
echo $fullActionName;
Mage::log($fullActionName, null,'custom.log');
@dave-swift
Copy link

Or, Mage::app()->getFrontController()->getAction()->getFullActionName()

@vishy93
Copy link

vishy93 commented Jan 1, 2019

Or, Mage::app()->getFrontController()->getAction()->getFullActionName()

@dave-swift I am using this but get the error:

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

When using this 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);
 
?>`

My observer:

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);
 			}           
 	}
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment