• Eyelash / php jquery image management
  • Product specials on home page

    This tutorial explains briefly how to display one product on sale on the right column of thehome page, but that could be any page in fact.The code below is inspired from the front page featured product tutorial.

    • Step 1 : create the product special block  class/ Note that the getSpecialProducts function is inspired from the sales catalog list extension
      create the file app\code\local\PoleOuest\Catalog\Block\Product\Special.php

      1. <?
      2. class PoleOuest_Catalog_Block_Product_Special extends Mage_Catalog_Block_Product_Abstract
      3. {
      4. protected function _getStoreId()
      5. {
      6. $storeId = null;
      7. if($storeId == null) {
      8. $storeId = Mage::app()->getStore()->getId();
      9. }
      10. return $storeId
      11. }
      12. protected function _getCustomerGroupId()
      13. {
      14. $custGroupID = null;
      15. if($custGroupID == null) {
      16. $custGroupID = Mage::getSingleton(‘customer/session’)->getCustomerGroupId();
      17. }
      18. return $custGroupID;
      19. }
      20.  
      21. // POLEOUEST Method from salescatalog
      22. public function getSpecialProducts()
      23. {
      24. if (is_null($this->_productCollection)) {
      25. $storeId = $this->_getStoreId();
      26. $websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
      27. $custGroup = $this->_getCustomerGroupId();
      28. $product = Mage::getModel(‘catalog/product’);
      29. $todayDate = $product->getResource()->formatDate(time(), false);
      30. $rulePriceWhere = "({{table}}.rule_date is null) or ({{table}}.rule_date=’$todayDate’ and {{table}}.website_id=’$websiteId’ and {{table}}.customer_group_id=’$custGroup’)";
      31. $productAttributes = array_flip(Mage::getSingleton(‘catalog/config’)->getProductAttributes());
      32. unset($productAttributes[‘price_type’]);
      33. unset($productAttributes[‘price_view’]);
      34. $productAttributes = array_flip($productAttributes);
      35. $specials = $product->setStoreId($storeId)->getResourceCollection()
      36. ->addAttributeToFilter(‘special_price’, array(‘gt’=>0), ‘left’)
      37. ->addAttributeToFilter(‘special_from_date’, array(‘date’=>true, ‘to’=> $todayDate), ‘left’)
      38. ->addAttributeToFilter(array(
      39. array(‘attribute’=>‘special_to_date’, ‘date’=>true, ‘from’=>$todayDate),
      40. array(‘attribute’=>‘special_to_date’, ‘is’ => new Zend_Db_Expr(‘null’))
      41. ), , ‘left’)
      42. ->addAttributeToSort(‘special_from_date’, ‘desc’)
      43. ->addAttributeToSelect($productAttributes, ‘inner’)
      44. ->joinTable(‘catalogrule/rule_product_price’, ‘product_id=entity_id’, array(‘rule_price’=>‘rule_price’, ‘rule_start_date’=>‘latest_start_date’, ‘rule_date’=>‘rule_date’), $rulePriceWhere, ‘left’)
      45. ;
      46.  
      47. $specials->getSelect()->order(new Zend_Db_Expr(‘RAND()’))->limit(1); ;
      48. $rulePriceCollection = Mage::getResourceModel(‘catalogrule/rule_product_price_collection’)
      49. ->addFieldToFilter(‘website_id’, $websiteId)
      50. ->addFieldToFilter(‘customer_group_id’, $custGroup)
      51. ->addFieldToFilter(‘rule_date’, $todayDate)
      52. ;
      53.  
      54. $productIds = $rulePriceCollection->getProductIds();
      55. if (!empty($productIds)) {
      56. $specials->getSelect()->orWhere(‘e.entity_id in (‘.implode(‘,’,$productIds).‘)’);
      57. }
      58.  
      59. Mage::getSingleton(‘catalog/product_status’)->addVisibleFilterToCollection($specials);
      60. Mage::getSingleton(‘catalog/product_visibility’)->addVisibleInCatalogFilterToCollection($specials);
      61. $this->_productCollection = $specials;
      62. }
      63. return $this->_productCollection;
      64. }
      65. }
      66. ?>
    • Step 2 : add new block to your etc/config.xml in global/blocks/catalog
      <rewrite>
      <product_special>PoleOuest_Catalog_Block_Product_Special</product_special>
      </rewrite>
    • Step 3 : create your template catalog/product/PoleOuestSpecial.phtml with the loop inspired from the catalog/product/list.phtml
    • Step 4 : declare your block in your layout/catalog.xml in the right.permanent.callout block
      <block type=”catalog/product_special” name=”product_special” as=”product_special” template=”catalog/product/PoleOuestSpecial.phtml” />
    • Step 5 : call your child HTML where you want it in \template\callouts\right_col.phtml :
      <?php echo $this->getChildHtml(‘product_special’) ?>

    and let us know how it went for you

    1. One Response to “Product specials on home page”

    2. May 13, 2009 - Anton Maryanov

      Hey buddy, thanks for your article. It helped me a lot ;) also it will be better if you do not use these ugly quotes when publish your code :)
      also you can add these
      Mage::getSingleton(‘catalog/product_status’)->addSaleableFilterToCollection($specials); Mage::getSingleton(‘cataloginventory/stock’)->addInStockFilterToCollection($specials);

      for showing only available and salable products.
      And the last:
      ->addAttributeToSelect($productAttributes, ‘inner’) – this does not work for me(I have empty results), only ‘left’ join works well.

    Quick contact form