1

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.

3

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"

  • 1
    If you're not a relying on a Data Model Object, better use an Helper, that better suit this use case. – dagfr Aug 1 '13 at 14:42
  • +1 Thanks for the reply and that tip. For my use it was dependant on a Data Model Object which is why I used it as a model. I just put this this example as the answer so it's easy to understand. I'm assuming that if it was used as a Helper then it could be called in a similar way with Mage::getHelper('name_space/customfunctions')->myRandomFunction($foo); ? – James Aug 1 '13 at 23:49
  • 1
    Yes, it's Mage::helper('name_space/customfunctions')->yourfunction($foo); and the helper goes in the Helper folder not Model ^^ – dagfr Aug 2 '13 at 6:58

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.