简体   繁体   English

Magento:构建库存中所有可配置产品的定制产品系列

[英]Magento: Build custom product collection of all configurable products in stock

I'm trying to build a product collection of all configurable products which are 'in-stock' or 'is-saleable'. 我正在尝试构建所有可配置产品的产品系列,这些产品是“库存”或“可销售”。 These require two different models to be used. 这些需要使用两种不同的型号。 My working method is: 我的工作方法是:

$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')->addAttributeToFilter('type_id', array('eq' => 'configurable'));

foreach ($collectionConfigurable as $_configurableproduct) {
    $product = Mage::getModel('catalog/product')->load($_configurableproduct->getId());

    if ($product->isSaleable()) { 
// do something
}
}

However this script is REALLY slow and I have a feeling it's wasting resources running as it will be loading and going through EVERY configurable product. 然而,这个脚本真的很慢,我觉得它浪费资源运行,因为它将加载并通过每个可配置的产品。

What I'm trying to achieve is to get the $collectionConfigurable and make it in-stock items only. 我想要实现的是获得$ collectionConfigurable并仅使其成为库存商品。

Another resource cites this as a method to get in stock items. 另一种资源将此作为获取库存物品的方法。

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

But I'm not sure how to combine them or use it properly, I've tried this: 但我不确定如何将它们合并或正确使用它,我试过这个:

$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')->addAttributeToFilter('type_id', array('eq' => 'configurable'));
$instockConfigs = Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collectionconfigurable);

This returns with the following error: 返回时出现以下错误:

  Fatal error: Call to a member function joinField() on a non-object in /srv/magento/app/code/core/Mage/CatalogInventory/Model/Resource/Stock.php on line 197

My "naive to the finer details of the stock system" approach would be to 我的“对股票系统更精细细节的天真”方法将是

  1. Create a stock item collection, grabbing only the in stock items. 创建库存项目集合,仅获取库存项目。

  2. Use that collection to create an array of instock product IDs 使用该集合创建一个instock产品ID数组

  3. Create a product collection with the configurable filter, as well as an entity_id filter using the collected product IDs 使用可配置过滤器创建产品集合,并使用收集的产品ID创建entity_id过滤器

The code for that would look like this. 这个代码看起来像这样。

//create a stock item collection with a `is_in_stock` filter
$collection = Mage::getModel('cataloginventory/stock')
->getItemCollection()
->addFieldToFilter('is_in_stock');

//capture the product ids of the in stock stock items
$product_ids = array();
foreach($collection as $item)
{
    $product_ids[] = $item->getProductId();
}

$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('entity_id',array('in'=>$product_ids))
->addFieldToFilter('type_id','configurable');

foreach($products as $product)
{
    var_dump($product->getData());
}

That said, your code is slow, in part, because you're reloading each product inside the loop, generating a new series of SQL statements 也就是说,你的代码很慢,部分是因为你在循环中重新加载每个产品,生成一系列新的SQL语句

$product = Mage::getModel('catalog/product')->load($_configurableproduct->getId());

Also, the addInStockFilterToCollection only works with a Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection collection. 此外, addInStockFilterToCollection仅适用于Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection集合。 Take a look at the doc block on the method. 看一下方法的doc块。

/**
 * Adds filtering for collection to return only in stock products
 *
 * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection $collection
 * @return Mage_CatalogInventory_Model_Stock $this
 */
public function addInStockFilterToCollection($collection)
{
    $this->getResource()->setInStockFilterToCollection($collection);
    return $this;
}

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

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