Magento - 只加载可配置的产品

2021-12-19 00:00:00 php magento

我有以下代码:

$_productCollection = $this->getLoadedProductCollection();

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

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

While it does what it's supposed to do, it greatly slows down page load time. Is it possible to load only configurable products and remove the check for 'configurable'? The store has 12000 products, about 700 are configurable and the rest are child simple products.

我发现以下代码返回所有可配置的产品.我只需要当前类别中的产品:

I found the following code which returns all configurable products. I need only the products within the current category:

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

推荐答案

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

The problem with getLoadedProductCollection() is it's already loaded - the products' data has already been retrieved from the database. Just using the current category's product collection isn't good enough either, that will ignore the "layers" (attribute filters). The trick is to remove the loaded products from the list first.

// 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();

print_r($_productCollection) 也有问题,您不仅要输出产品,还要输出作为数据库连接的资源的所有详细信息、缓存值以及产品的个体资源等等...

print_r($_productCollection) has it's issues too, you're not just outputting the products but also all details of the resource that is the database connection, and cached values, and the products' individual resources, and so on...

在这种情况下,我认为您会更满意:

In this case I think you would be happier with:

print_r($_productCollection->toArray())

相关文章