在我的网站上,我有几家商店。一个特定的商店既包含简单和可配置的产品。这是一大批产品。什么是获取所有启用产品的最快方法 (including child products of configurable products) 通过商店ID收集这家商店?

注意:我尝试了许多不同的方式。但是要花很多时间。有时使服务器崩溃。

注意:我正在使用 Magento CE 1.3

任何建议将不胜感激。

有帮助吗?

解决方案

$category = Mage::getModel('catalog/category')->load($categoryId);    
$collection = Mage::getModel('catalog/product')->getCollection()
        ->addCategoryFilter($category);
//you can use the next lines to add some minimal information about the products to the collection
//thanks @DavidManners
$collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) //add list attributes
            ->addMinimalPrice() //add prices
            ->addFinalPrice()
            ->addTaxPercents()
//or
//$collection->addAttributeToSelect('*');//if you want all the attributes
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);//only enabled products
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);//only visible products

现在问题。通常,与可配置的产品相关的简单产品与类别无关,或者设置为不可见。如果您不是这种情况,那么上面的代码就足够了。如果它们与类别相关联,但不可见,请评论最后一行。
否则,您必须循环浏览产品,当您找到可配置的产品时,将其获得关联的产品:

//this is needed if you want to get the out of stock simple products also
Mage::helper('catalog/product')->setSkipSaleableCheck(true);
$allProducts = array();
foreach ($collection as $product){
    $allProducts[] = $product;
    if ($product->getTypeId() == 'configurable'){
        $simpleProducts = $product->getTypeInstance(true)
                ->getUsedProducts(null, $product);
        foreach ($simpleProducts as $simple){
            $allProducts[] = $simple;
        }
    }
}

现在您应该拥有所有需要的产品 $allProducts 大批。
由于获得了简单的相关产品,这可能不会很快,但是应该为您带来所需的东西。我希望它不会崩溃服务器。

其他提示

为了添加@marius的答案,要限制可以使用resource_iterator类的二手资源。

Mage::getSingleton('core/resource_iterator')->walk($collection->getSelect(), array('productCallback'), array());

function productCallback($args)
{
  var_dump($args['row']);
  [...]
}

现在,当时将处理一个结果,而不是立即执行查询并立即检索所有结果,然后继续进行下一个结果。

许可以下: CC-BY-SA归因
scroll top