如何按库存订购产品?这是我的查询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;
我试着把这段代码:
->joinLeft(array('bs'=>'cataloginventory/stock_status'), 'bs.product_id = e.entity_id', array('stock_status' => 'bs.stock_status'))->order("stock_status desc")
但它没有成功。我希望首先看到的产品库存高于其他产品。
答案 0 :(得分:0)
使用代码下面的产品集合上的联接 -
$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');