Magento - load only configurable products(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 - 只加载可配置的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Magento - 只加载可配置的产品
基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 使用 PDO 转义列名 2021-01-01