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.

  • are you u want to show the special on left navigation>?> – Amit Bera Aug 12 '14 at 16:39
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;

  • 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 – James Roeiter Jul 26 '15 at 22:53
  • Pagination will be broken by doing this. – mimarcel Oct 27 '15 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

  • 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 Aug 13 '14 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 Aug 13 '14 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 Aug 13 '14 at 14:51
  • You can try and share results with the community :) – Amasty Aug 13 '14 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, privacy policy and cookie policy

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