Description
This was a problem in 1.x Magento as well.
$productAttribute->getFrontend()->getSelectOptions(); basically grabs all available options, regardless if they are relevant to the product. Not a big issue if you have a small set of super attributes but if you have an amount numbering in the thousands or more, it can cause additional time on on any for loops that are using the returning data.
All credit to turnkeye (http://turnkeye.com/blog/magento-perfomance-optimization-of-configurable-products)
Implementing these changes saves us around 300ms per product in an add to cart call (we have around 2,000 super attributes) or any cart save call.
Mage_Catalog_Model_Resource_Product_Type_Configurable_Attribute_Collection
protected function _loadPrices() (line 266)
Replace:
$options = $productAttribute->getFrontend()->getSelectOptions();
With:
$_options = array();
foreach ($_prods as $associatedProduct) {
$_options[] = $associatedProduct->getData($productAttribute->getAttributeCode());
}
$options = $productAttribute->getSource()->getNeededOptions($_options)
Mage_Eav_Model_Entity_Attribute_Source_Table
Add function:
public function getNeededOptions($ids) {
$storeId = $this->getAttribute()->getStoreId();
$collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setPositionOrder('asc')
->setAttributeFilter($this->getAttribute()->getId())
->addFieldToFilter('main_table.option_id', array('in' => $ids))
->setStoreFilter($this->getAttribute()->getStoreId())
->load();
return $collection->toOptionArray();
}
This logic can also be applied to $value->getSource()->getOptionText($attributeValue); which also calls $this->getAllOptions(false) and loads all of the options unnecessarily.