Question

After having to work on someone else's Magento webshop, I found a huge performance issue. The shop consists of configurable products. In the overview, an attribute from the simple products is shown. However, the person who made this before me, used getUsedProducts which caused enormous load times. I've been trying to optimise the script, but am still not satisfied.

The current code is the following:

            <?php 
            // First check if the product is configurable
            if($_product->isConfigurable()) {
                // get the necessary model
                $_helper2 = Mage::getModel('catalog/product_type_configurable')
                            ->setProduct($_product);
                $_subproducts = $_helper2
                                ->getUsedProductCollection()
                                ->addAttributeToSelect('color')
                                ->addFilterByRequiredOptions(); // the biggest speedbump

                // initialise our colours array
                $_colors = array();
                $_color_id = array();

                // look for attribute value of color in each associated product and assign to $_colours array
                foreach ($_subproducts as $_subproduct) {
                  // look for attribute value of color in each associated product and assign to $_colours array

                  $label = $_subproduct->getAttributeText('color');
                  $_colors[] = $label;

                  if (!isset($_color_id[$label])) {
                    $_color_id[$label] = $_subproduct->getData('color');
                  }
                }

                // remove dup's
                $_color_swatch = array_unique($_colors);

                echo '<ul class="color-swatch clearer">';

                foreach($_color_swatch as $_inner_option_id){
                    echo '<li class="color-li color-li-id-' . $_color_id[$_inner_option_id] . '"><div class="option-color color-id-' . $_color_id[$_inner_option_id] . '"><div class="option-color-desc">' . $_inner_option_id . '</div></div></li>';
                }

                echo '</ul>';
            }

            ?>

It get's the color attribute from each simple product, removes duplicates, and places one color swatch for each color. However, this still get's quite a big loading time, because it still loads too much information I think.

Could somebody point me in the right direction to create a better way of loading the attributes of the simple products?


Edit: IS there a better way of loading the attributes?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top