Welcome to our Magento blog!

With a rich variety of products and selling campaigns, it seems to be a challenge for both sellers and buyers to manage their information. This brings me to the idea of finding the way to list of products on sale and sorts them by discount percent or saved value.

Step 1: You create a sell-off list with the following code:

<!--?php<br /--> $today = date('Y-m-d',time());
$productCollection = Mage::getResourceModel('catalog/product_collection');
$productCollection-&gt;addAttributeToSelect(Mage::getSingleton('catalog/config')
-&gt;getProductAttributes())
-&gt;addAttributeToFilter('special_price',array('is' =&gt; new Zend_Db_Expr('not null')))
-&gt;addAttributeToFilter('special_from_date', array('or'=&gt; array(
0 =&gt; array('date' =&gt; true, 'to' =&gt; $today),
1 =&gt; array('is' =&gt; new Zend_Db_Expr('null')))
), 'left')
-&gt;addAttributeToFilter('special_to_date', array('or'=&gt; array(
0 =&gt; array('date' =&gt; true, 'from' =&gt; $today),
1 =&gt; array('is' =&gt; new Zend_Db_Expr('null')))
), 'left')
-&gt;addAttributeToFilter(
array(
array('attribute' =&gt; 'special_from_date', 'is'=&gt;new Zend_Db_Expr('not null')),
array('attribute' =&gt; 'special_to_date', 'is'=&gt;new Zend_Db_Expr('not null'))
)
)
-&gt;addMinimalPrice()
-&gt;addTaxPercents()
-&gt;addStoreFilter();

Mage::getSingleton('catalog/product_status')
-&gt;addVisibleFilterToCollection($productCollection);
Mage::getSingleton('catalog/product_visibility')
-&gt;addVisibleInSearchFilterToCollection($productCollection);

Step 2: you can sort those products by discount percent by adding the code below:


$productCollection->getSelect()
->order('((`price_index`.`price` - `at_special_price`.`value`)/`price_index`.`price`) DESC');

Or sort them by the saved value


$productCollection->getSelect()
->order('(`price_index`.`price` - `at_special_price`.`value`) DESC');

Hope it helpful for you! If you are interested in other knowledge in Magento framework, read in Magento blog.

Author

Alex is the CTO & Co-founder of Magestore which has been providing retail solutions for Magento merchants since 2009. He started as a Magento developer just one year after the release of the first version of Magento. With over 10 years experience of working with Magento and clients all over the world, he gained great knowledge on e-commerce development, order management systems, inventory control & retail POS.

6 Comments

  1. Hi. It is possible to put it just to CMS page like this:{{block type= caatlog/product_list name= specials as= specials template= templatename.phtml }}but how can I handle number of products per page?When I normally have 30 products pre page and 20 has no special price the result will show only 10 products. The worse thing is that the number of products is on every page different.

    • Hello Natalya,

      You can insert this code to the end of that block for handling number of shown products: $productCollection->getSelect()->limit(10);

      In this case, there are 10 products shown on. Nice code!

  2. Hi,
    I’m new in magento as you mention create sell-off following code can you tell me path of this file where can put this code.I have done all process but don’t get any proper solution.

    • You can insert this code to the end of that block you want to list and sort on-sale

Write A Comment