How to get all super attribute options for a configurable item in Magento
I have configurable products in my system consisting of color an开发者_如何学God size. I've written the following code to get the data but it is far too slow. Before adding this bit of code the page load time is below 2 seconds and after adding it jumps to 15 seconds. Surely there is faster way to get this information (I have 2 super attributes with about 10 options each)
My code:
$productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
$attributeOptions = array();
foreach ($productAttributeOptions as $productAttribute) {
foreach ($productAttribute['values'] as $attribute) {
$attributeOptions[$productAttribute['label']][$attribute['value_index']] = $attribute['store_label'];
}
}
The operation getConfigurableAttributesAsArray is very slow. This is because it performs a load on the attribute collection.
Best way to fix this is using a custom query which only fetches the data you need. Something like this:
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$result = $read->query(
"SELECT eav.attribute_code FROM eav_attribute as eav
LEFT JOIN catalog_product_super_attribute as super ON eav.attribute_id = super.attribute_id
WHERE (product_id = " . $this->getProduct()->getId() . ");"
);
$attributeCodes = array();
while ($row = $result->fetch()) {
$attributeCodes[] = $row['attribute_code'];
}
This will fetch all attribute codes. You can change the query so that will fetch the data you need.
精彩评论