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->addAttributeToSelect(Mage::getSingleton('catalog/config') ->getProductAttributes()) ->addAttributeToFilter('special_price',array('is' => new Zend_Db_Expr('not null'))) ->addAttributeToFilter('special_from_date', array('or'=> array( 0 => array('date' => true, 'to' => $today), 1 => array('is' => new Zend_Db_Expr('null'))) ), 'left') ->addAttributeToFilter('special_to_date', array('or'=> array( 0 => array('date' => true, 'from' => $today), 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')) ) ) ->addMinimalPrice() ->addTaxPercents() ->addStoreFilter(); Mage::getSingleton('catalog/product_status') ->addVisibleFilterToCollection($productCollection); Mage::getSingleton('catalog/product_visibility') ->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.
6 Comments
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!
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.
Hi
Where can i put this code ? I’m new one in magento
You can insert this code to the end of that block you want to list and sort on-sale
Hi,
Can you tell me where i can put this code,please ?