filtering product collection on “is_salable”

笑着哭i 提交于 2019-11-30 05:02:06

There is no is_salable attribute in the product so it will rise an exception. If you want to display only products that are in stock, use this stock model addInStockFilterToCollection method:

Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($this->_productCollection);

try this as well...

$stockCollection = Mage::getModel('cataloginventory/stock_item')->getCollection()->addFieldToFilter('qty', array('gteq' => 1))->addFieldToFilter('type_id', 'simple');

addFieldFilter ('qty',array('gteq' =>1))

get all products collection that have a stock of 1 or greater, you can put any number here according to your needs

addFieldToFilter('type_id', 'simple')

filter by simple products

I stumbled upon the problem that isSaleable() always returns false on my products, the problem was that I was getting the collection but didn't add the attribute price to the collection. In the isSaleable() function there is a check to make sure the price isn't 0, it needs the price attribute to check that so:

Changing:

$collection = Mage::getModel('catalog/product')->getCollection();
        $collection->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('url_path')
                ->addAttributeToSelect('status')
                ->addUrlRewrite();

To:

$collection = Mage::getModel('catalog/product')->getCollection();
        $collection->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('price')
                ->addAttributeToSelect('url_path')
                ->addAttributeToSelect('status')
                ->addUrlRewrite();

Did the trick :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!