Question

I am building an admin module which extends the sales order mass actions.

I am at the point where I have added my module to the mass action dropdown, and am receiving the post data in my controller.

My question is what is the correct method to add in my custom functions that process and return data back to the controllers and where would I store them in best practice?

Eg if I am collecting the post data in my controller like so:

$orderIds = $this->getRequest()->getPost('order_ids', array());

and then I go through the orders like so and want to use a custom...

foreach ($orderIds as $orderId){
            $order = Mage::getModel('sales/order')->load($orderId);
            echo myCustomFunction($orderId);
        }

Where would I store this custom function, and how would I call it into the controller? Usually outside of Magento I would create something like a functions.php file and put an include_once but I have a feeling that isn't the best way to go with the Magento architecture.

Was it helpful?

Solution

The trick was to create another file in the Models folder called in this case CustomFunctions.php

Which starts like this

class Name_Space_Model_CustomFunctions
{
//Add in all custom functions here

public function myRandomFunction($variable){
$response = "This function has been passed the variable - " . $variable;
return $response;
}

}

Then in one of the controllers you can access it like this:

$foo = "bar";
$answer = Mage::getModel('name_space/customfunctions')->myRandomFunction($foo);
echo $answer;

echo $answer would put out "This function has been passed the variable - bar"

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top