views:

105

answers:

1

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, getting an error:

Invalid attribute name: manage_stock.

Help? Matt

A: 

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.

dan.codes
thanks dan - will give this a shot in a bit and report back. cheers
Matt
nice one dan - this did the trick. cheers
Matt
also, +1 for answering my first q on stackoverflow! :)
Matt