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 […]

Magento – Put Category-IDs into category tree in backend

To show the category IDs in the category tree under Catalog->Categories->Manage Categories you can rewrite Mage_Adminhtml_Block_Catalog_Category_Tree in an own module. You have to rewrite the method buildNodeName like this: /** * Get category name * * @param Varien_Object $node * @return string */ public function buildNodeName($node) { $result = $this->escapeHtml($node->getName()); if ($this->_withProductCount) { $result .= ‘ (‘ . $node->getProductCount() […]

Magento – Define default values in config.xml

You can define system config value defaults in your modules config.xml like that. <?xml version=”1.0″?> <default> <myconfig_group> <settings> <test>1</test> <cronjobs>0</cronjobs> <client>0813</client> <serviceprovider>117</serviceprovider> <pricelist></pricelist> </settings> <productexport> <lux_kategorie>21</lux_kategorie> <store_view_german>1</store_view_german> <store_view_english>12</store_view_english> </productexport> </myconfig_group> </default> This sets the default value for all stores. If you want to set default values for a special store (scope), you can do it […]

Own Router or Controller

To get an own ActionController that can be called like this: http://www.mysexydomain.de/nicefrontname/nicecontrollerprefix/niceactionprefix/ … you have to add the following snippet into the FE or BE section of your config.xml: <frontend> <routers> <sns_documentmaster> <use>standard</use> <args> <module>SNS_Documentmaster</module> <frontName>nicefrontname</frontName> </args> </sns_documentmaster> </routers> </frontend>  

Event Observer

The following snippet have to be added to the config.xml under the frontend (for FE-events) – or the adminhtml (for BE-Events) – tag: <events> <event_name> <observers> <observer_unique_config_name> <!– singleton or model –> <type>singleton</type> <class>Namespace_Modul_Model_Modelname</class> <method>functionToCallOnModelClassInstance</method> </observer_unique_config_name> </observers> </event_name> </events>  

Add JavaScript or CSS in Block class

 To add JavaScript or CSS files to the head section of the resulting HTML page, you can add something like this to the _prepareLayout function of a Block class: $headBlock = $this->getLayout()->getBlock(‘head’); $headBlock->addJs(‘somefolder/ask-sheldon.js’); $headBlock->addCss(‘somefolder/ask-sheldon.css’); As you can see, we can use the layout model to get the head block an use its functions to add files. This […]