我有以下代码:
$_productCollection = $this->getLoadedProductCollection();
foreach ($_productCollection as $_product)
{
if ($_product->_data['type_id'] == 'configurable')
{
...
}
}
虽然它做了它应该做的事情,但它大大减慢了页面加载时间。是否可以仅加载可配置产品并删除“可配置”检查?该商店有 12000 种产品,其中约 700 种是可配置的,其余为 child 简单产品。
我发现以下代码返回所有可配置的产品。我只需要当前类别中的产品:
$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'configurable'));
最佳答案
getLoadedProductCollection()
的问题在于它已经加载 - 产品数据已从数据库中检索。仅使用当前类别的产品集合也不够好,这将忽略“层”(属性过滤器)。诀窍是先从列表中删除加载的产品。
// 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->toArray())
关于php - Magento - 仅加载可配置产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5280392/