問題描述
我有以下代碼:
$_productCollection = $this->getLoadedProductCollection();
foreach ($_productCollection as $_product)
{
if ($_product->_data['type_id'] == 'configurable')
{
...
}
}
雖然它做了它應(yīng)該做的事情,但它大大減慢了頁(yè)面加載時(shí)間.是否可以僅加載可配置產(chǎn)品并取消對(duì)可配置"的檢查?商店有12000種產(chǎn)品,約700種可配置,其余為兒童簡(jiǎn)單產(chǎn)品.
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.
我發(fā)現(xiàn)以下代碼返回所有可配置的產(chǎn)品.我只需要當(dāng)前類別中的產(chǎn)品:
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()
的問題是它已經(jīng)加載了 - 已經(jīng)從數(shù)據(jù)庫(kù)中檢索了產(chǎn)品的數(shù)據(jù).僅使用當(dāng)前類別的產(chǎn)品集合也不夠好,這將忽略層"(屬性過濾器).訣竅是首先從列表中刪除加載的產(chǎn)品.
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)
也有問題,您不僅要輸出產(chǎn)品,還要輸出作為數(shù)據(jù)庫(kù)連接的資源的所有詳細(xì)信息、緩存值以及產(chǎn)品的個(gè)體資源等等...
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...
在這種情況下,我認(rèn)為您會(huì)更滿意:
In this case I think you would be happier with:
print_r($_productCollection->toArray())
這篇關(guān)于Magento - 只加載可配置的產(chǎn)品的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!