Magento Code and Programming Practice

    Magento addAttributeToFilter:

    Get all product collection with the below code:

    Make the choice of items through a collection that consistently provides you a comprehensive array of products in collection. This can make better association and improved market value that you can make known on the database you implement.

    $collection = Mage::getModel('catalog/product')->getCollection();


    Add different types of filters on collection. Example shown below:

    //  Equal To (eq)

    $collection->addAttributeToFilter('status', array('eq' => 1));

    //  Not Equal To (neq)

    $collection->addAttributeToFilter('visibility', array('neq' => 1));

    // Greater Than (gt)

    $collection->addAttributeToFilter('price', array('gt' => 3.99));

    // Less Than (lt)

    $collection->addAttributeToFilter('price', array('lt' => 3.99));

    // Greater Than or Equal To (gteq)

    $collection->addAttributeToFilter('price', array('gteq' => 4.99));

    // Less Than or Equal To (lteq)

    $collection->addAttributeToFilter('price', array('lteq' => 4.99));

    // Contains (like) - also uses % wildcards

    $collection->addAttributeToFilter('sku', array('like' => 'DVD%'));

    // Does Not Contain (nlike) - also uses % wildcards

    $collection->addAttributeToFilter('sku', array('nlike' => 'ABC%'));

    // In Array (in)

    $collection->addAttributeToFilter('id', array('in' => array(1,3,12)));

    // Not In Array (nin)

    $collection->addAttributeToFilter('id', array('nin' => array(1,2,12)));

    // Is NULL (null)

    $collection->addAttributeToFilter('description', 'null');

    // Is Not NULL (notnull)

    $collection->addAttributeToFilter('description', 'notnull');

    Magento Snippets for Development

    Magento Product Thumbnail Image Switcher:

    Thumbnail image switchers that are icons, which you click on when you want to proceed to change procedures laid out with use of this snippet gets another image lined up. In addition, another code fragment used to attain the addresses of all customers who you engage with to have a choice of accessing them when you want to consult for transaction purposes.


    Go to file media.phtml



    Find the code below

    <a href="#" onclick="popWin('<?php echo $this->getGalleryUrl($_image) ?>', 'gallery', 'width=300,height=300,left=50,top=50,location=no,status=yes,scrollbars=yes,resizable=yes'); return false;">


    Replace it with

    <a href="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()); ?>" title="<?php echo $_product->getName();?>" onclick="$('image').src = this.href; return false;">

       <!--nested img tag stays the same-->


    Get Collection of All Customer Addresses In Magento:

    Use following code when you intend to retrieve all addresses of customers with a knowhow of their email ID.


    $websiteid = Mage::app()->getwebsite->getid();

    $store = Mage::app()->getStore();

    $customer = Mage::getModel("customer/customer");

    $customer->website_id = $websiteid;



    $address = Mage::getModel("Customer/Entity_Address_Collection");




    Coding through Magento Snippets

    Getting Top and Sub Categories in Magento:

    Avail top, sub categories through the Magento development code snippet below, and see different distinctions showing your particulars with certain names mentioned in your database. In addition, display category bestsellers are another group you can use, as they are always being on display on the database, which show after execution of the following code snippet.

    Place this code in you top.phtml (\app\design\frontend\base\default\template\catalog\navigation)

    <?php $_helper = Mage::helper('catalog/category') ?>

    <?php $_categories = $_helper->getStoreCategories() ?>

    <?php $currentCategory = Mage::registry('current_category') ?>

    <div id="navMainWrap">

    <?php if (count($_categories) > 0): ?>

    <ul id="navMain"><?php foreach($_categories as $_category): ?>

    <li class="navLevel1" id="navJewelry">

    <a class="navLevel1Link" href="<?php echo $_helper->getCategoryUrl($_category) ?>">

    <?php echo $_category->getName() ?>


    <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>

    <?php $_subcategories = $_category->getChildrenCategories() ?>

    <?php if (count($_subcategories) > 0): ?>

    <div class="navSubWrap">

                <div class="navSub clearFix">

                    <ul class="col1 clearFix">

    <li><p class="colHead">

    Shop by Category</p>


    <?php foreach($_subcategories as $_subcategory): ?>


    <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">

    <?php echo $_subcategory->getName() ?>



    <?php endforeach; ?>           </ul>

    <div class="navSubBnr">





    <?php endif; ?>


    <?php endforeach; ?>   </ul>

    <?php endif; ?>


    Magento Display Category BestSellers:

    $curr_categoryid = Mage::registry('current_category')->getId();

    $cat_Id= $curr_categoryid;

    $category = Mage::getModel('catalog/category')->load($cat_Id);

    $products = Mage::getResourceModel('reports/product_collection')



            ->setOrder('ordered_qty', 'desc')


    Display GrandTotal Of Shopping Cart in Magento:

    Grand total of shopping carts when you make purchases through an ecommerce site to buy through web shops or online retailers.


    $total = $quote->getGrandTotal();

    $total = $this->helper('checkout')->formatPrice($total);


  • (no comments)

Free Website Created & Hosted with Website.com Website Builder

Create Yours

Create Free Website Now

Stunning Website Templates. Free Domain.
Create a free website with website.com website builder. Start My Website