I have already good and working code which sorted my category list by stock_status. Other words i have out of stock products at bottom. But i also need to insert some code before my for sorting that part of products which out of stock only.
For example:
Product1 have Qty=0 and product_views attribute=97
Product2 have Qty=0 and product_views attribute=902
Product3 have Qty=0 and product_views attribute=75
Product4 have Qty=35 and product_views attribute=1500
Product5 have Qty=398 and product_views attribute=5298
Product6 have Qty=275 and product_views attribute=17
Right now i have this results if i sort by name:
Product4
Product5
Product6
Product1
Product2
Product3
This is amazing for me because i need to sort in stock products by name (or any other choice in frontend as usually) but in same time i need to sort out of stock products by product_views attribute.
And I need to have the list sorted like this (if i choose in the frontend sort by name for example):
Product4
Product5
Product6 (this is in stock products that because it must be sorted as usually based on choice of selectbox in frontend)
Product2
Product1
Product3 (this is out of stock products that because it must be sorted by product_views attrubute - most viewed in top)
Right now i use this code for second level sorting (and i ASK YOU TO hELP ME find the 1 level sorting by product_views. But must be sorted the out of stock products only):
try {
$websiteId = Mage::app()->getStore()->getWebsiteId();
if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
$stockStatusFieldExisted = Mage::helper('webpiercom_outofstockmastmndmostviewed_catalog')->checkFieldExisted($collection->getSelect(), 'stock_status');
if(!$stockStatusFieldExisted) {
$collection->joinTable(
array('wprdc' => 'cataloginventory/stock_status'),
'product_id=entity_id',
array('stock_status'),
array('website_id' => $websiteId),
'left'
);
}
}
$collection->getSelect()->order('stock_status desc');
}