0

I'm new to magento(i'm using 1.8 btw), I currently have price and color filters on category page and they are working fine, but I can't find any help for adding a new filter to show only products that currently have a special price.

I don't want a specific page to show all "special price" products, I need this to be done with a filter, on the category page.

1
  • are you u want to show the special on left navigation>?>
    – Amit Bera
    Commented Aug 12, 2014 at 16:39

3 Answers 3

1

Here's how I solved the problem:

Probably not the best solution, but it's working...

On my product/list.phtml I check if I want to show the filter or not, then if yes:

$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

$category = new Mage_Catalog_Model_Category();

$category->load(Mage::getModel('catalog/layer')->getCurrentCategory()->getId());

$collection = $category->getProductCollection();

$collection
->addAttributeToSelect('*')
->addAttributeToFilter('special_from_date', array('or'=> array(
        0 => array('date' => true, 'to' => $todayDate),
        1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter('special_to_date', array('or'=> array(
        0 => array('date' => true, 'from' => $todayDate),
        1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter(
        array(
                array('attribute' => 'special_from_date', 'is'=>new Zend_Db_Expr('not null')),
                array('attribute' => 'special_to_date', 'is'=>new Zend_Db_Expr('not null'))
        )
)
->addAttributeToSort('special_from_date', 'asc')
->setVisibility(
        Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds()
);

$_productCollection = $collection;

2
  • I dont think that is the correct solution , as the collection in the list.phtml arrives already after the filtering/ordering process of the controller and block , so you are actually performing filtering/ordering on already filtered / ordered data . For example , if you have 500 items but only 9 are displayed per page , this code I think will only run on the 9 items of the specific page as determined by toolbar.php Commented Jul 26, 2015 at 22:53
  • Pagination will be broken by doing this.
    – mimarcel
    Commented Oct 27, 2015 at 12:37
0

Special price is also attribute like the color. You can enable it for filtering via admin - catalog - attributes - manage attributes - special price - show in layered navigation

4
  • This would work, but I would like to show only products that currently have a special price. Using it the way you said, it show all products with special price even if they don't have a special price anymore. Is there a way to do this? Maybe changing some backend code?
    – user13141
    Commented Aug 13, 2014 at 12:59
  • Yes, you are right. But it is not very easy. You need to rewrite the apply method from the catalog/layer/filer/attribute, check the attribute code, and if it is the special price, add date-from / date-to limitation.
    – Amasty
    Commented Aug 13, 2014 at 14:10
  • Would this work: (?) $_productCollection = $this->getLoadedProductCollection(); if($isSpecialPrice){ _productCollection ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $dateToday)) ->addAttributeToFilter('special_to_date', array('date' => true, 'from' => $dateTomorrow)); }
    – user13141
    Commented Aug 13, 2014 at 14:51
  • You can try and share results with the community :)
    – Amasty
    Commented Aug 13, 2014 at 16:32
0
$category = new Mage_Catalog_Model_Category();

$category->load(Mage::getModel('catalog/layer')->getCurrentCategory()->getId());

$collection = $category->getProductCollection();

$collection
->addAttributeToSelect('*')
->addAttributeToFilter('special_from_date', array('or'=> array(
        0 => array('date' => true, 'to' => $todayDate),
        1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter('special_to_date', array('or'=> array(
        0 => array('date' => true, 'from' => $todayDate),
        1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToFilter(
        array(
                array('attribute' => 'special_from_date', 'is'=>new Zend_Db_Expr('not null')),
                array('attribute' => 'special_to_date', 'is'=>new Zend_Db_Expr('not null'))
        )
)
->addAttributeToSort('special_from_date', 'asc')
->setVisibility(
        Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds()
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.