OpenMage / magento-lts

Official OpenMage LTS codebase | Migrate easily from Magento Community Edition in minutes! Download the source code for free or contribute to OpenMage LTS | Security vulnerability patches, bug fixes, performance improvements and more.
https://www.openmage.org
Open Software License 3.0
870 stars 436 forks source link

Suboptimal method Mage_Catalog_Model_Product_Type_Grouped::getAssociatedProducts #1218

Open midlan opened 4 years ago

midlan commented 4 years ago

Summary (*)

The method Suboptimal method Mage_Catalog_Model_Product_Type_Grouped::getAssociatedProducts is not optimal at all.

Examples (*)

    public function getAssociatedProducts($product = null)
    {
        if (!$this->getProduct($product)->hasData($this->_keyAssociatedProducts)) {
            $associatedProducts = array();

            if (!Mage::app()->getStore()->isAdmin()) {
                $this->setSaleableStatus($product);
            }

            $collection = $this->getAssociatedProductCollection($product)
                ->addAttributeToSelect('*') //problem #1
                ->addFilterByRequiredOptions()
                ->setPositionOrder()
                ->addStoreFilter($this->getStoreFilter($product))
                ->addAttributeToFilter('status', array('in' => $this->getStatusFilters($product)));

            foreach ($collection as $item) { //problem #2
                $associatedProducts[] = $item;
            }

            $this->getProduct($product)->setData($this->_keyAssociatedProducts, $associatedProducts);
        }
        return $this->getProduct($product)->getData($this->_keyAssociatedProducts);
    }

Proposed solution

I would:

  1. depracate the method
  2. refactor all usages of it to use Mage_Catalog_Model_Product_Type_Grouped::getAssociatedProductCollection with only needed attributes.
Genaker commented 4 years ago

M2 Optimisation of this method:

https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/GroupedProduct/Model/Product/Type/Grouped.php#L203-L227