Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$productAttribute->getFrontend()->getSelectOptions() grabbing unnecessary options #293

Closed
fr0x opened this issue May 9, 2013 · 2 comments

Comments

@fr0x
Copy link

fr0x commented May 9, 2013

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.

@magento-team
Copy link
Contributor

Please check #90 for detailed discussion regarding this issue.

@fr0x
Copy link
Author

fr0x commented Jun 3, 2013

The problem that this issue fixes is the number of super attributes returned by this line:
$options = $productAttribute->getFrontend()->getSelectOptions();

Issue #90 does nothing to correct that.

I'm assuming you may have run a similar test against this change (as in issue #90):
"2500 configurable products with 2 simple products as options per each configurable product"

How many super attributes did this test contain? Was each simple product completely unique in this test (therefore having 5,000 super attributes)? If you only had 2 super attributes to cover the 5,000 simple products, then this test would have (incorrectly) shown no improvements.

If you have 5,000 super attributes, this call:
$options = $productAttribute->getFrontend()->getSelectOptions();

is going to return 5,000 options causing the for loop (foreach ($options as $option)) to run 5,000 times.

Even worse, the nested for loop (foreach ($usedProducts as $associatedProduct)) is going to cause this to run even more than than the already unnecessary 5,000 times.

While both issues are addressing the same function and the same issue, they are addressing them in different ways (and the test case used to test issue #90 might have been completely inaccurate depending on the number of super attributes that it was tested against).

magento-team pushed a commit that referenced this issue May 22, 2015
magento-team pushed a commit that referenced this issue Jan 13, 2016
mmansoor-magento pushed a commit that referenced this issue Aug 26, 2016
…ETWO-54785-State-province-not-required-mainline

[Fearless kiwis] MAGETWO-54785: [GitHub] State/Province field doesn't show as required on the add new address page. #5279
magento-engcom-team pushed a commit that referenced this issue Feb 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants