How can I get sale products(special price) on category view page. Products must be from same category or sub-categories. Thanks.

有帮助吗?

解决方案 2

The easiest way is to make a duplicate of the List block and add the filter you need.

You May need to modify your attribute in the Magento admin area to "show in product listing" too.

for example:

{{block type="catalog/product_list" template="catalog/product/list.phtml"}}

this will use the List block to filter the collection for you, lets make a copy:

app/code/core/Mage/Catalog/Block/Product/List.php to

app/code/local/Mage/Catalog/Block/Product/Mylist.php Now lets modify the Block to use our custom attribute, something like this should work (not tested)

Mylist.php

class Mage_Catalog_Block_Product_Mylist extends Mage_Catalog_Block_Product_List
{
    /**
     * Retrieve loaded category collection
     *
     * @return Mage_Eav_Model_Entity_Collection_Abstract
     */
    protected function _getProductCollection()
    {
        $collection = parent::_getProductCollection();
        $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $collection->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');

        return $collection;
    }
}

Now you simple use your new block:

{{block type="catalog/product_mylist" template="catalog/product/list.phtml"}}

Thanks to(ref by): How to filter product list by custom attribute on category page of Magento?

其他提示

First of all you have to edit list.phtml from

app/design/frontend/YOURPACKAGE/YOURTHEME/template/catalog/product/list.phtml

and write below code at starting of file

$_productCollection=$this->getLoadedProductCollection() // Remember you have to comment this line

Mage::getSingleton('core/session', array('name' => 'frontend'));
$_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addMinimalPrice()
->addStoreFilter();

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($_productCollection);

$todayDate = date('m/d/y');
$tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
$tomorrowDate = date('m/d/y', $tomorrow);

 $currentCategory = Mage::registry('current_category');



 $_productCollection->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
    ->addAttributeToFilter('special_to_date', array('or'=> array(
    0 => array('date' => true, 'from' => $tomorrowDate),
    1 => array('is' => new Zend_Db_Expr('null')))
    ), 'left')
->addAttributeToFilter('category_id', array(
    array('finset' => $currentCategory->getId()))
;

and comment this line

$_productCollection=$this->getLoadedProductCollection();

Let me know if you have any query

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top