php - Order products by stock on Magento -


how can order products stock? 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 put code: ->joinleft(array('bs'=>'cataloginventory/stock_status'), 'bs.product_id = e.entity_id', array('stock_status' => 'bs.stock_status'))->order("stock_status desc")

but didn't work out. want see products higher stock first other products.

use 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'); 

Comments