开发者

In Magento, how do you filter a product collection by the manage_stock field?

I've built an inventory update script - where I fetch a product collection in Magento, and iterate through the result set, updating product inventory (based on a separate inventory feed) as I go.

I can fetch the product collection no problem.

However, I only want to get products which have the "Manage Stock" field (a dropdown in the admin under the "inventory" tab) set to "yes".

So I tried:

// get all magento catalog products with "manage stock" field set to yes
$items = Mage::getModel('catalog/product')->getCollection();
$items
 ->addAttributeToSelect(array(
  'id',
  'sku'
 ))
 ->addFieldToFilter(array(
  array(
   'attribute' => 'manage_stock',
   'eq' => '1'
  ),
 ));

But, ge开发者_开发百科tting an error:

Invalid attribute name: manage_stock.


Hey Matt, instead of using the addFieldToFilter you will probably have to use soemthing like this

$items->joinField('manages_stock','cataloginventory/stock_item','use_config_manage_stock','product_id=entity_id','{{table}}.use_config_manage_stock=1');

manage_stock is not a attribute on a product but on the actual stock item.

UPDATE:

That assumed that your config settings were set to manage stock and that no one ever set it to not use the config setting but make it manage stock.

I added this

$items->joinField('manages_stock','cataloginventory/stock_item','use_config_manage_stock','product_id=entity_id','{{table}}.use_config_manage_stock=1 or {{table}}.manage_stock=1');

I think this should be correct and accounts for any user error because if you have the config set to manage stock then if you don't want to manage stock you should just say use config settinges, but a user could set it to yes without checking that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜