简体   繁体   English

在Magento中,如何通过manage_stock字段过滤产品集合?

[英]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. 我建立了一个库存更新脚本-在这里,我在Magento中获取产品集合,并遍历结果集,并在进行时更新产品库存(基于单独的库存提要)。

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". 但是,我只想将“管理库存”字段(“库存”标签下的admin下拉菜单)设置为“是”的产品。

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. 无效的属性名称:manage_stock。

Hey Matt, instead of using the addFieldToFilter you will probably have to use soemthing like this 嗨,马特,您可能不得不使用类似这样的东西,而不是使用addFieldToFilter

$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. manage_stock不是产品的属性,而是实际的库存项目。

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. 我认为这应该是正确的,并且可以解决任何用户错误,因为如果您将配置设置为管理库存,那么如果您不想管理库存,则应该说使用配置设置,但是用户可以将其设置为“是”而无需检查那。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM