I’ve added a few scripts on my blog, and also over at Branded3 Labs previously, but I’ve not noted how I load them currently.
I’ve dabbled with adding scripts to the shell
folder in the past, and this seems like a logical place to keep them sometimes. However, I have got into the habit of using a _script
folder in the root of the application.
From this folder, we can include Magento, set up some configuration details, and some helpers, and run some code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/********************************************** // activate categories // for Magento 1.7.2 **********************************************/ error_reporting(E_ALL); ini_set('memory_limit', '8024M'); require_once '../app/Mage.php'; Mage::app()->setCurrentStore(0); umask(0); $categories = Mage::getModel('catalog/category') ->getCollection() ->addAttributeToSelect('*'); foreach($categories as $category) { // load the category echo $category->getId() . ',/' . $category->getUrlKey() . '/,' . $category->getUrl() . "\n"; } |
This is a quick example of how to list out all categories in the system.
First, we set the error reporting level so that we can quickly see any issues. If this is being run on production, you’ll probably want to disable this – or, remove the line so that the default setting take over.
Once done, we set a high memory limit, and then, we include the main Magento application file. Once we’ve included the main Magento application file (Mage.php), we have access to all things Magento, and we kick it off by initialising Magento and setting the current store.
Getting a list of categories is easy enough, and we can then manipulate it any way we wish.
Image Credit: kevin dooley
Comment or tweet @douglasradburn