Magento Expert Forum - Improve your Magento experience

Results 1 to 3 of 3

How to Show Products on Homepage Using Magento Filter Collection By Attribute,Field

  1. #1
    Junior Member Magento MageSolution's Avatar
    Join Date
    Dec 2013
    Posts
    203
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Lightbulb How to Show Products on Homepage Using Magento Filter Collection By Attribute,Field

    This tutorial will show you how to display new product, featured product, bestseller product or by attribute of product… on homepage.

    First, explain how Magento filter a collection. Use this to explain how Magento filter a collection by attribute, field in a model, so that people can learn to write their own collections.
    addAttributeToFilter() is a function that can be called on a collection in Magento.
    In short, it adds a condition to the WHERE part of the MySQL query used to extract a collection from the database.
    For example:

        $_products = Mage::getModel('catalog/product')->getCollection()
         ->addAttributeToSelect(array('name', 'product_url',
    'small_image'))
         ->addAttributeToFilter('sku', array('like' => 'UX%'))
         ->load();


    The above code would get a product collection, with each product having it’s name, url,
    price and small image loaded in it’s data array. The product collection would be filtered and contain only products that have an SKU starting with UX.

    Filtering

    We can filter our collections with the addFieldToFilter() function. In SQL you can think of these as your WHERE clauses.
    Lets say we want to get a product by the sku PRODUCT001

    $products = Mage::getModel('catalog/product')->getCollection();
    $products->addAttributeToFilter('sku','PRODUCT001');
    $product = $products->getFirstItem();


    Lets try the Greater Than conditional. Simply supply an array with the conditional type and value.
    Products greater than $100:

    $products = Mage::getModel('catalog/product')->getCollection();
    $products->addAttributeToFilter('price', array('gt' => '100'));


    Magento gives us lots of filter conditionals to choose from see: addAttributeToFilter
    Conditionals

    Read full lesson at : http://www.magesolution.com/blog/sho...ion-attribute/

    View more threads in the same category:


  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    100
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Thanks for posting those great points. It will help the Magento users a lot.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Using Featured Products Extension might be a easier and simpler for your site. No need to worry about technical issue. you will get support from technical team of the magento extension provider. And It is FREE, why don't you give it a try

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •