Magento - 仅加载可配置产品

Vin*_*ent 13 php magento

我有以下代码:

$_productCollection = $this->getLoadedProductCollection();

foreach ($_productCollection as $_product)
{
  if ($_product->_data['type_id'] == 'configurable')
  {
    ...
  } 
}
Run Code Online (Sandbox Code Playgroud)

虽然它做了它应该做的事情,但它大大减慢了页面加载时间.是否可以仅加载可配置产品并删除"可配置"检查?该商店有12000种产品,约700种可配置,其余为儿童简单产品.

我找到了以下代码,它返回所有可配置的产品.我只需要当前类别中的产品:

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

clo*_*eek 27

问题getLoadedProductCollection()是它已经加载 - 产品的数据已经从数据库中检索出来了.仅使用当前类别的产品集合也不够好,这将忽略"图层"(属性过滤器).诀窍是首先从列表中删除已加载的产品.

// First make a copy, otherwise the rest of the page might be affected!
$_productCollection = clone $this->getLoadedProductCollection();
// Unset the current products and filter before loading the next.
$_productCollection->clear()
                   ->addAttributeToFilter('type_id', 'configurable')
                   ->load();
Run Code Online (Sandbox Code Playgroud)

print_r($_productCollection) 也有它的问题,你不仅要输出产品,还要输出资源的所有细节,即数据库连接,缓存值,产品的个人资源等等......

在这种情况下,我认为你会更高兴:

print_r($_productCollection->toArray())
Run Code Online (Sandbox Code Playgroud)


Fre*_*d K 7

所有这些解决方案都不适合我,试试这个:

$_productCollection1 = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id','configurable'); 

foreach ($_productCollection1 as $product1) {
    echo $product1->getName();
    ...
}
Run Code Online (Sandbox Code Playgroud)

它有效,但不知道它是否正确(我是Magento的新手).请告诉我.