Magento - 只加载可配置的产品

Magento - load only configurable products(Magento - 只加载可配置的产品)
本文介绍了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 - 只加载可配置的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)