我在自定义 Magento 控制器中做了以下方法来检索指定类别中的所有制造商。该模块作为一项服务来获取 ajax 调用的数据。
我做了很多这样的方法,所有方法都在我的本地服务器上执行,时间为 5-7 秒。这需要14 秒在本地服务器上执行。
你能帮我在这里找到一个瓶颈吗:
public function subcategoryAction() {
$storeId = Mage::app()->getStore()->getStoreId();
// Subcategory ID passed with a GET method
$sub = $this->getRequest()->getParam('subcategory');
if ($sub) {
// Querying to get all product ID's in the specified subcategory
$product_ids = Mage::getResourceModel('catalog/product_collection')
->setStoreId($storeId)
->addAttributeToFilter('status', array('eq' => '1'))
->addAttributeToFilter('visibility', 4)
->addCategoryFilter(Mage::getModel('catalog/category')
->load($sub))->getAllIds();
$product = Mage::getModel('catalog/product');
// Load all the product models by their ID's
foreach ($product_ids as $id) {
$product->load($id);
$manufacturers[] = $product->getAttributeText('manufacturer');
}
// Getting unique values of manufacurers, just like array_unique
$manufacturers[$product->getAttributeText('manufacturer')] = $product->getAttributeText('manufacturer');
// Echoing default option value
echo "<option value='all'>BRAND/MAKE</option>";
// Echoing and formatting manufacturers for a dropdown
foreach ($manufacturers as $manufacturer) {
if ($manufacturer != "") {
echo "<option value='" . $manufacturer . "'>" . $manufacturer . "</option>";
}
}
}
}
接受@Mischa Leiss 的建议,更改了这个凌乱的唯一值代码:
$manufacturers=array_flip(array_flip(array_reverse($manufacturers,true)));
到他的代码:
$manufacturers[$product->getAttributeText('manufacturer')] = $product->getAttributeText('manufacturer');
解决方案
这是最快的解决方案,感谢@Mischa
$products = Mage::getResourceModel('catalog/product_collection')
->setStoreId($storeId)
->addAttributeToSelect('manufacturer')
->addAttributeToFilter('status', array('eq' => '1'))
->addAttributeToFilter('visibility', 4)
->addCategoryFilter(Mage::getModel('catalog/category')
->load($sub));
只需大约 2 秒。