**Welcome to the Playground, here we play**

Last modified by andy101010 on Mon, March 24, 2008 16:51
Source|Old Revisions  

This is an old revision of the document!


I’ve been working with Magento version 0.8.16100 for a couple days now trying to implement a Featured Product feature. The Featured Product is a product with an attribute I added from the admin. When the administrator selects “Yes” in the “Featured” attribute, that product will be displayed in a content block.

I’ll explain each step I took to make this custom feature. Please forgive me if I left anything out. The PHP itself I would like to rewrite to be more... professional, but I couldn’t figure out how to query a product by its attribute. Maybe you can help?


Create a new attribute by going to Catalog > Attributes > Manage Attributes > Add New Attribute.

Attribute Properties

  • Attribute Identifier: featured
  • Catalog Input Type for Store Owner: Yes/No
  • Unique Value (not shared with other products): No
  • Values Required: No
  • Input Validation for Store Owner: None
  • Apply To Configurable/Grouped Product: Yes

Front End Properties

  • Use in quick search: No
  • Use in advanced search: Yes
  • Comparable on Front-end: No
  • Use In Layered Navigation (Can be used only with catalog input type ‘Dropdown’): No

System Properties

  • Data Type for Saving in Database: String
  • Globally Editable: No
  • Visible on Catalog Pages on Front-end: Yes

Manage Label/Options

  • Default: Featured Product
  • English: Featured Product

Save the new attribute and go to Catalog → Attributes → Manage Attributes Sets to add the attribute to the default feature set.


Open MyCompany/app/design/frontend/default/default/layout/catalog.xml. We want to add a new <block> right above the product list block in the default category layout.

Insert the block configuration on line 73 (default catalog.xml).

  <block type="catalog/product_featured" name="product_featured" as="product_featured" template="catalog/product/featured.phtml"></block>

Create a new file, and directories: app/code/local/MyCompany/Catalog/Block/Product/Featured.php

  class MyCompany_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract
  {
      public function getFeaturedProduct()
      {
          // instantiate database connection object
          $storeId = 0;
          $categoryId = $this->getRequest()->getParam('id', false);
          $resource = Mage::getSingleton('core/resource');
          $read = $resource->getConnection('catalog_read');
          $productStoreTable = $resource->getTableName('catalog/product_store');
          $categoryProductTable = $resource->getTableName('catalog/category_product');
      
          // Query database for all products in this category because I don't know how to query by attribute
          // this logic bothers me because I'm selecting and instantiating each product in the whole category
          // just to get the first featured product.  The query should include the 'featured' attribute in the WHERE
          // clause.  If you know how to do that, please share :)
          $select = $read->select()
                         ->from(array('cp'=>$categoryProductTable))
                         ->join(array('ps'=>$productStoreTable), 'ps.product_id=cp.product_id', array())
                         ->where('ps.store_id=?', $storeId)
                         ->where('cp.category_id=?', $categoryId);
          $featuredProductData = $read->fetchAll($select);
      
          // iterate over result set from database
          foreach ($featuredProductData as $row) {
          
              // instantiate the product object
              $product = Mage::getModel('catalog/product')->load($row['product_id']);
          
              // if the product is a featured product, return the object
              if ($product->getData('featured')) {
                  return $product;
              }
          }
      
          return;
      }
  }

We’re almost there!


Create a new file, and directories, called app/code/local/MyCompany/Catalog/Block/Category/View.php. We’re extending the core class here so our module will be separate from the core code base. When upgrading, we won’t have to worry about our code not working or having to patch files.

  class MyCompany_Catalog_Block_Category_View extends Mage_Catalog_Block_Category_View
  {
      public function getFeaturedProductHtml()
      {
          return $this->getBlockHtml('product_featured');
      }
  }

I haven’t really integrated any real HTML changes, so I will simply point out the files to create and where to edit the HTML.

Edit app/design/frontend/default/default/template/catalog/category/view.phtml and add the following code:

  <?=$this->getFeaturedProductHtml()?>

right above this line:

  <?=$this->getProductListHtml()?>

Edit app/design/frontend/default/default/template/catalog/product/featured.phtml and customize your HTML here (see any of the product templates for Product object member variables).


Add

  <blocks>
      <catalog>
          <rewrite>
              <product_featured>MyCompany_Catalog_Block_Product_Featured</product_featured>
         </rewrite>
          <rewrite>
              <category_view>MyCompany_Catalog_Block_Category_View</category_view>
          </rewrite>
       </catalog>
  </blocks>

I hope this helps you add a “Featured Product” feature. It certainly feels thorough, but if I left anything out, please let me know and I’ll be happy to help. If you know how to query by a specific product attribute, please let me know.

Thanks,

Andy




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs