加载简单产品集合(有库存和无库存)


9

将可配置产品的所有“子”产品(包括缺货)加载到集合中时出现问题。

像这样加载产品:

$simpleCollection = $configurable->getUsedProductCollection()
    ->addAttributeToSelect('*')
    ->addFilterByRequiredOptions();

foreach ($simpleCollection as $simple) {
   //$simple->getName();
}

会忽略缺货的子产品,这可能是因为未加入价格表的子产品。

有没有加载所有子ID的另一个选项getChildrenIds然后加载每个简单的产品负载

Answers:


2

问题出在addStoreFilter()in中getUsedProductCollection()

public function getUsedProductCollection($product = null)
{
    $collection = Mage::getResourceModel('catalog/product_type_configurable_product_collection')
        ->setFlag('require_stock_items', true)
        ->setFlag('product_children', true)
        ->setProductFilter($this->getProduct($product));
    if (!is_null($this->getStoreFilter($product))) {
        $collection->addStoreFilter($this->getStoreFilter($product));
    }

    return $collection;
}

这将添加过滤器以仅显示当前商店中可销售的产品。

如果$configurable是可配置产品的类型实例,则可以在调用之前取消设置商店过滤器,如下所示getUsedProductCollection()

$configurable->setStoreFilter(null);

完整的解决方案:

$configurable = $product->getTypeInstance();

$configurable->setStoreFilter(null);
$simpleCollection = $configurable->getUsedProductCollection()
    ->addAttributeToSelect('*')
    ->addFilterByRequiredOptions();

foreach ($simpleCollection as $simple) {
   //$simple->getName();
}

1

如果您尝试这种方式会发生什么:

$simpleCollection = $configurable->getUsedProductCollection()
                     ->addAttributeToSelect('*')
                     //->addFilterByRequiredOptions() //don't use any filter, get all itmes
                     ;


foreach($simpleCollection as $simple){
   //$simple->getName();
}

试试看。


1

您可以使用以下代码作为此问题的替代解决方案:

$simpleCollection=$configurable->getTypeInstance(true)
                ->getUsedProducts(null,$configurable);

foreach($simpleCollection as $simple){
   //$simple->getName();
}

1

如果您需要与可配置产品相​​关联的产品集合,则应该可以进行以下操作:

$configurableProduct = Mage::getModel('catalog/product')->load(<your_product_id>);
$associatedProducts = $configurableProduct->getTypeInstance()->getUsedProductCollection($configurableProduct);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.