I have a custom page that I have put together that basically shows products that are on sale. I was asked if I could sort the开发者_运维知识库 products coming out of the collection by the highest percentage of discount.
So I am wondering if it is possible to set the order of products based on the mathematical result of diving two other attributes together.
Take a look at Smart Sorting Extension for Magento.
It lets your customers sort products in your store’s catalogue in 9 new ways:
Now in Wishlists (real time),
Now in Carts (real time),
Biggest $ Saving,
Biggest % Saving,
Customers’ Choice,
Most Discussed,
Most Appreciated,
Most Recent,
Most Viewed.
I think I figured it out. Alan Storm previously helped me with a similar problem I was having sorting products in an arbitrary way.
Magento get a product collection in an arbitrary order
Using the same idea I came up with the following code to make this work:
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('price')
->addAttributeToSelect('small_image')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4)
->addAttributeToFilter('special_price', array('neq' => ""))
->addAttributeToFilter('discontinued', array('neq' => 1))
->setPageSize($results_per_page)
->setCurPage($current_page)
;
$products->getSelect()->order('(`e`.special_price / `e`.`price`)', 'DESC');
Credit should go to Alan Storm on this one.
use: $products->getSelect() ->order('1 - (price_index.final_price / e.price)', 'DESC');
so even if you have some rules promotions active there are considered.
D. Militos answer was correct for me, with a slight correction:
$products->getSelect() ->order('1 - (price_index.final_price / price_index.price)', 'DESC');
精彩评论