問題描述
我有以下代碼:
$_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 - 只加載可配置的產品的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!