Friday, 8 July 2011

how to get onsale products in magento?

[Step 1] Create a Onsale.php file and put it here :
app/code/local/Mage/Catalog/Block/Product/Onsale.php

class Mage_Catalog_Block_Product_Onsale extends Mage_Catalog_Block_Product_Abstract{
    public function __construct(){
        parent::__construct();
        $storeId    = Mage::app()->getStore()->getId();
        $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addStoreFilter()
            ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
            ->addAttributeToFilter('special_to_date', array('or'=> array(
                0 => array('date' => true, 'from' => $todayDate),
                1 => array('is' => new Zend_Db_Expr('null')))
            ), 'left')
            ->addAttributeToSort('special_from_date', 'desc')
            ->setPageSize($this->getProductsCount())
            ->setCurPage(1);
        $this->setProductCollection($collection);
    }
}

[Step 2] Create a onsale.phtml file and put it here :
app/design/frontend/*/*/template/catalog/product/onsale.phtml

getProductCollection()) && $product_->getSize()): ?>
__('On Sale Now!') ?>
getItems()); echo $_collectionSize; ?>

[Step 3] now put this line where you want to view onsale products..

{{block type="catalog/product_onsale" template="catalog/product/onsale.phtml"}}

Source : http://harisur.net/how-to-get-onsale-products-in-magento

No comments:

Post a Comment