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 - 只加载可配置的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)