cancel
Showing results for 
Search instead for 
Did you mean: 

How to sort the products which are out of stock if I already sorted the list of products in function

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

How to sort the products which are out of stock if I already sorted the list of products in function

Hi

I have working code which moving all out of stock products at bottom, now i need to add some code for sorting the out of stock products only which is already at bottom of the list products in the category before placing its there.

 

i have this in Layer.php

class WebPierCom_OutOfStockLastAndMostViewed_Catalog_Model_Layer extends Mage_Catalog_Model_Layer
{
public function prepareProductCollection($collection)
{
parent::prepareProductCollection($collection);
if (!Mage::helper('webpiercom_outofstockmastmndmostviewed_catalog')->isSortOutOfStockProductsAtBottomEnabled()) {
return $this;
}
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');
} 
catch (Exception $e) {}
return $this;
}
}


And i have in Helper.php this

 

 

class WebPierCom_OutOfStockLastAndMostViewed_Catalog_Helper_Data extends Mage_CatalogInventory_Helper_Data
{
const XML_PATH_SORT_OUT_OF_STOCK = 'cataloginventory/options/sort_out_of_stock_at_bottom';
const XML_PATH_SORT_OUT_OF_STOCK_BY_MOST_VIEWED = 'cataloginventory/options/sort_out_of_stock_at_bottom_by_most_viewed';
const XML_PATH_SORT_OUT_OF_STOCK_SEARCH_RESULT = 'cataloginventory/options/sort_out_of_stock_at_bottom_for_search';

public function isSortOutOfStockProductsAtBottomEnabled()
{
return $this->isShowOutOfStock() && Mage::getStoreConfigFlag(self::XML_PATH_SORT_OUT_OF_STOCK);
}
public function isSortOutOfStockProductsAtBottomByMostViewedEnabled()
{
return $this->isShowOutOfStock() && Mage::getStoreConfigFlag(self::XML_PATH_SORT_OUT_OF_STOCK_BY_MOST_VIEWED);
}
public function isEnabledForSearchResults()
{
return $this->isShowOutOfStock() && Mage::getStoreConfigFlag(self::XML_PATH_SORT_OUT_OF_STOCK_SEARCH_RESULT);
}
public function checkFieldExisted($select, $field)
{
$result = false;
if($field) {
$columns = $select->getPart(Zend_Db_Select::COLUMNS);
foreach ($columns as $column) {
if (in_array($field , $column)) {
$result = true;
break;
}
} 
}
return $result;
}
}
1 REPLY 1

Re: How to sort the products which are out of stock if I already sorted the list of products in func

Try this code it will sort the product collection based on the inventory Quantity.

$collection = Mage::getResourceModel('catalog/product_collection');$collection->getSelect()->joinLeft(            array('_inventory_table'=>'cataloginventory_stock_item'),
            "_inventory_table.product_id = e.entity_id ",            array('qty')
        )->order(array('_inventory_table.qty DESC'));

You can change the DESC to ASC to do the reverse