Magento Expert Forum - Improve your Magento experience

Results 1 to 2 of 2

Filter products by attribute on a Magento CMS page

  1. #1
    Administrator david's Avatar
    Join Date
    Nov 2012
    Posts
    260
    Thanks
    23
    Thanked 40 Times in 33 Posts

    Post Filter products by attribute on a Magento CMS page

    Ability to create pages with custom content is a great feature provided by Magento. You can easily create a new page for your store and add text, html, images, different widgets there. But sometimes people want to have something non-trivial on their pages. Usually it means that you need to operate some knowledge before youll get an appropriate result. In this article we would like to suggest you a simple solution on how to place a products list, prefiltered by some specific attribute, on your CMS page. The example below describes how to create a simple extension for this purpose.

    Lets call the extension Atwix_Cmsattr. First, you need to add the init file for you extension. The path is app/etc/modules/Atwix_Cmsattr.xml . The files content is:

    HTML Code:
    <config>
        <modules>
            <Atwix_Cmsattr>
                <active>true</active>
                <codePool>local</codePool>
            </Atwix_Cmsattr>
        </modules>
    </config>
    The extension contains one block class and one model, therefore the config file, which you should have by the following path: app/code/local/Atwix/Cmsattr/etc/config.xml, is pretty simple and consists of a few records:

    HTML Code:
    <config>
        <modules>
            <Atwix_Cmsattr>
                <version>0.1.0</version>
            </Atwix_Cmsattr>
        </modules>
        <global>
            <blocks>
                <atwix_cmsattr>
                    <class>Atwix_Cmsattr_Block</class>
                </atwix_cmsattr>
            </blocks>
            <models>
                <atwix_cmsattr>
                    <class>Atwix_Cmsattr_Model</class>
                </atwix_cmsattr>
            </models>
        </global>
    </config>
    After that, create the block class app/code/local/Atwix/Cmsattr/Block/List.php:

    PHP Code:
    <?php
    class Atwix_Cmsattr_Block_List extends Mage_Catalog_Block_Product_Abstract
    {
        protected 
    $_itemCollection null;
     
        public function 
    getItems()
        {
            
    $color $this->getColor();
            if (!
    $color)
                return 
    false;
            if (
    is_null($this->_itemCollection)) {
                
    $this->_itemCollection Mage::getModel('atwix_cmsattr/products')->getItemsCollection($color);
            }
     
            return 
    $this->_itemCollection;
        }
    }
    and the model class app/code/local/Atwix/Cmsattr/Model/Products.php:

    PHP Code:
    <?php
    class Atwix_Cmsattr_Model_Products extends Mage_Catalog_Model_Product
    {
        public function 
    getItemsCollection($valueId)
        {
            
    $collection $this->getCollection()
                ->
    addAttributeToSelect('*')
                ->
    addAttributeToFilter('color', array('eq' => $valueId));
            
    Mage::getSingleton('cataloginventory/stock')->addStockFilterToCollection($collection);
            return 
    $collection;
        }
    }
    The next file is the most personal thing. You need the template file with your own mockup corresponding to your design. Here is an example for a simple list (app/design/frontend/base/default/template/atwix/cmsattr/list.phtml):

    PHP Code:
    <?php $_items $this->getItems() ?>
    <div class="block">
        <div class="block-title">
            <strong><span><?php echo $this->__('Red Products'?></span></strong>
        </div>
        <div class="block-content">
            <ol class="mini-products-list">
            <?php foreach ($_items as $_item): ?>
                <li class="item">
                    <div class="product">
                        <a href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_item'thumbnail')->resize(50?>" width="50" height="50" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" /></a>
                        <div class="product-details">
                            <p class="product-name"><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->htmlEscape($_item->getName()) ?></a></p>
                            <?php echo $this->getPriceHtml($_itemtrue?>
                            <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <a href="<?php echo $this->getAddToWishlistUrl($_item?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist'?></a>
                            <?php endif; ?>
                        </div>
                    </div>
                </li>
            <?php endforeach; ?>
            </ol>
        </div>
    </div>
    The last thing you need to do is to create a CMS page and insert the products list where you want to show it on the page. So, go to Admin->CMS->Pages->Add New Page. Fill the page with the necessary content and connect our products block anywhere in the content by putting the following string:

    Code:
    {{block type="atwix_cmsattr/list" name="cmsattr" template="atwix/cmsattr/list.phtml" color="26"}}
    Note, that you need to pass the attribute value for color here. The product list on this page is filtered by this value. If you going to filter product by text attribute, such as name for example, you can pass the text value directly like name=Apple. But current example describes how to filter by attribute with type select. There are many ways to get value id from attributes value text. One of the simple (non programmatically) ways to do this is to inspect attributes values form using your browser. The example below describes how to do it within Google Chrome:



    Also you can extend your models logic with the new method, which would transform attribute value text into its id, but this small article doesnt describe how to do it

    View more threads in the same category:


  2. #2
    Junior Member simon.walker's Avatar
    Join Date
    Apr 2013
    Location
    Austin, Texas
    Posts
    82
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Thanks David for suach a nice information about filter products by attribute on a magento CMS page, but last month I have purchased an extension from fmeextensions.com/press-release.html and they have integrated this extension with my site. I will try to integrate this method in my new extension.

Similar Threads

  1. Adding new category attribute in Magento
    By golddev in forum Programming & Development
    Replies: 6
    Last Post: 29-11-2017, 02:21 AM
  2. How To Add A Static Block To A Category Page
    By rocker in forum Webmaster & Administrator
    Replies: 2
    Last Post: 17-08-2017, 10:17 AM
  3. Blank page after enabling Magento Compiler
    By david in forum Programming & Development
    Replies: 7
    Last Post: 29-10-2015, 07:04 AM
  4. Page link error!
    By indusstar777 in forum Programming Questions
    Replies: 3
    Last Post: 19-06-2013, 06:19 PM
  5. Replies: 0
    Last Post: 12-06-2013, 09:39 AM

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
  •