How can I order products by stock? This is my query atm:
$storeId = Mage::app()->getStore()->getId();
$cateids = $this->getCategoryId();
if($cateids) {
$catIds = explode(',', $cateids);
$arr_productids = $this->getProductIdsByCategories($catIds);
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addMinimalPrice()
->addUrlRewrite()
->addTaxPercents()
->addStoreFilter()
->addIdFilter($arr_productids)
->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->setOrder ($fieldorder,$order);
$products->getSelect()->order('rand()');
} else {
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addMinimalPrice()
->addFinalPrice()
->addStoreFilter()
->addUrlRewrite()
->addTaxPercents()
->setOrder ($fieldorder,$order);
$products->getSelect()->order('rand()');
}
$products->setPageSize($this->getProductCount())->setCurPage(1);
return $products;
I tried to put this code: ->joinLeft(array('bs'=>'cataloginventory/stock_status'), 'bs.product_id = e.entity_id', array('stock_status' => 'bs.stock_status'))->order("stock_status desc")
But it didn't work out. I want to see the products with higher stock first then the other products.
Use the join on product collection below code like-
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->joinField('qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left'
)->addAttributeToFilter('qty', array('gt' => 0))->setOrder('qty','DESC');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.