• Overriding a core model in Magento

    Posted on October 12, 2012 by Nimrod Techn

    Overriding a core model in Magento can be done by creating a Magento extension:

    Let's say we would like to override app/code/core/Mage/Newsletter/Model/Subscriber.php

    1. Create the company directory in local (app/code/local/Engineering).

    2. Create the extension directory inside (app/code/local/Engineering/Newsletter)

    3. Create model & etc directories inside (app/code/local/Engineering/Newsletter/model & app/code/local/Engineering/Newsletter/etc).

    model - what we would like to rewrite

    etc - the necessary configuration to tell Magento we're overriding a core model

    4. Copy the model there (Subscriber.php).

    5. Change the copied-model's class name to the following:  class Engineering_Newsletter_Model_Subscriber extends Mage_Newsletter_Model_Subscriber

    6. Create a config.xml file in app/code/local/Engineering/Newsletter/etc

    7. Place the following inside the config.xml:

    <?xml version="1.0"?>
    <config>
        <modules>
            <Engineering_Newsletter>
                <version>0.1.0</version>
            </Engineering_Newsletter>
        </modules>
        <frontend>
            <routers>
                <newsletter>
                    <args>
                        <modules>
                          <Engineering_Newsletter before="Mage_Newsletter">
                             Engineering_Newsletter
                          </Engineering_Newsletter>
                       </modules>
                    </args>
                </newsletter>
            </routers>
        </frontend>
        <global>
            <models>
                <newsletter>
                    <rewrite>
                        <subscriber>Engineering_Newsletter_Model_Subscriber</subscriber>
                    </rewrite>
                </newsletter>
            </models>
       </global>
    </config>

    * Bear in mind: the xml file is case-sensitive, and you'd better stick to the conventions (capitals wherever needed) - that's where alot of beginners waste hours of debugging, trying to understand where their mistake is.


    This post was posted in Writing a Magento Extension

  • Overriding Magento core

    Posted on October 12, 2012 by Nimrod Techn

    Repeat after me. we never, ever, ever, ever - change Magento's core files (app/code/core).

    Why? lots of reasons. the most important one is, we would like to upgrade our Magento system at some point.

    The best of practices would be to create a directory in app/code/local named Mage (app/code/local/Mage).

    Now copy the core file to the exact same directory hierarchy (create the same hierarchy), on local.

     

    Which means, if the file we would like to copy is at app/code/core/Mage/catalog/controllers/IndexController.php, we would copy it to app/code/local/Mage/catalog/controllers/IndexController.php.

    Blocks, controllers, helpers - can be copied like this without a problem.

    Models, however, require you to develop a Magento extension with slight modifications.

     

     


    This post was posted in Writing a Magento Extension

  • Get product name using its sku - in efficient way - using collection

    Posted on February 6, 2012 by admin

    If for some reason you have list of products skus, and for each you need to fetch out it's name,
    you will better not use the old way of
    $pname = Mage::getSingleton("catalog/product")->load(Mage::getSingleton("catalog/product")->getIdBySku($productSku))->getName();

    because it will slow up dramatically your site.
    Better use the following piece of code:

    <?php
    
    $productSku = 'sku1';
    
    $pname = Mage::getSingleton("catalog/product")
    	    		->getCollection()
    	    		->addFieldToFilter('sku', $newpsku)
    	    		->addAttributeToSelect('name')
    	    		->load()
    	    		->getFirstItem()
    	    		->getName();

    This post was posted in Speeding Up your Magento Website, Working With Magento Collections

Items 13 to 15 of 22 total

Page:
  1. 1
  2. ...
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
Magento is a well-engineered eCommerce platform designed to help engineers develop customized eCommerce online stores. Due to lack of proper coding documentation, Engineer-ing.com was created with the sole purpose of instructing Magento developers to-be with the "how-to-do" know-how. In the event of unresolved issues, you are more than welcome to contact me for consultation. However, please do so only if you possess a Software Engineering background and you're able to specify your question.