Magento-仅加载可配置产品 [英] Magento - load only configurable products

查看:66
本文介绍了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())

这篇关于Magento-仅加载可配置产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆