Add Content In Magento Controller

If you want to add content to the rendered page inside a controller action, you can do it like that: $this->loadLayout(); $message = $this->getLayout() ->createBlock(‘core/text’, ‘ready-block’) ->setText(‘<h1>Testaction for Controller:</h1>’); $this->getLayout()->getBlock(‘content’)->insert($message); $this->renderLayout();  

Magerun

Magerun is a very useful commandline tool for dealing with daily Magento development problem like: clean / flush caches reindexing the indices set configuration values in the database (core_config_data) shorthand query syntax for database accesses … It’s developed by netz98 (http://www.netz98.de/) and made my daily business so much easier. Installation (Linux) $> wget https://raw.githubusercontent.com/netz98/n98-magerun/master/n98-magerun.phar $> […]

Implement ACL (Access-Control-List) for own Magento modules

The following snippets show how to implement ACL (Access-Control-List) for system configuration fields and main menu entries of your own modules.  It is also shown, how to check the ACL’s in the PHP code of your Module. Implement ACL – Access control for system configuration If you had configured a system configuration (System→Configuration) like this: <?xml […]

Delete users in Magento frontend

If you need the possibility to let the users delete their own accounts, you can use the following snippet in a corresponding controller or observer to delete users in frontend: <?php $oCustomer = $this->_getSession()->getCustomer(); if(!$oCustomer->getId()){ Mage::throwException($this->__(‘Cannot load customer!’)); } //lie to mage 😉 // @todo: Check, if there is a better way Mage::register(‘isSecureArea’, true); $oCustomer->delete(); $this->_getSession()->clear(); […]

Image system configuration field

To add an image selector field to your system configuration: <image> <label>Image</label> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>0</show_in_store> <sort_order>70</sort_order> <frontend_type>image</frontend_type> <backend_model>adminhtml/system_config_backend_image</backend_model> <upload_dir config=”system/filesystem/media” scope_info=”1″>customer/responsible_contacts</upload_dir> <base_url type=”media” scope_info=”1″>customer/responsible_contacts</base_url> <show_in_default> <enabled>1</enabled> </show_in_default> </image>

System configuration fields

There are different field types for Magento’s system configuration. These are for example: Button Checkboxes Checkbox Date File Hidden Imagefile Image Label Link Multiline Multiselect Note Obscure Password Radio Radios Reset Select Submit Textarea Text Time The following snippets show you how to add them to your system.xml: Enable selection (yes/no): <?xml version=”1.0″?> <config> <sections> […]

Rewrite exiting Magento models

To rewrite exiting Magento models, you can add the following snippet to your modules config.xml: <?xml version=”1.0″?> <config> <modules> <AskSheldon_Module> <version>0.1.3</version> </AskSheldon_Module> </modules> <!– … –> <global> <models> <checkout> <rewrite> <session>AskSheldon_Module_Model_Checkout_Session</session> <type_onepage>AskSheldon_Module_Model_Checkout_Type_Onepage</type_onepage> </rewrite> </checkout> </models> </global> <!– … –> </config> In this case Mage_Checkout_Model_Session and Mage_Checkout_Model_Type_Onepage are rewritten.

Translation files for custom modules

To register a translation file for your module: <!– Translation file for FE –> <frontend> <translate> <modules> <Sheldon_Module> <files> <!– filename under app/locale/LANGUAGE_CODE/ f.e.: LANGUAGE_CODE = “EN_US” –> <default>filename.csv</default> </files> </Sheldon_Module> </modules> </translate> </frontend>   <!– Translation file for BE–> <adminhtml> <translate> <modules> <Sheldon_Module> <files> <!– filename under app/locale/LANGUAGE_CODE/ f.e.: LANGUAGE_CODE = “EN_US” –> <default>filename.csv</default> […]

Performance optimized attribute saving

Sometimes, when you have to handle many data-sets, it is more efficient to save only those attributes that have changed: // load product $product = Mage::getModel(“catalog/product”)->load(142);   // change attribute $product->setTitle(“Test”);   // tell resource attribute to save only the specified field $product->getResource()->saveAttribute($product, “title”);  

Add image product attribute

This is how to get a new image attribute to a given attribute set inside of a setup script (add image attribute for products): <?php /** * * Date: 5.12.2017 * Time: 0:58:0 * Last modified: 0:57:28 * Class / File: mysql4-upgrade-0.5.2-0.5.3.php * * Installs additional image attributes for customization * preview on product detail […]