4

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?

2 Answers 2

13

I have preferred to add groupByAttribute for single list of color

$ids=Mage::getResourceSingleton('catalog/product_type_configurable')
            ->getChildrenIds($parentId);


      $_subproducts = Mage::getModel('catalog/product')->getCollection()
    ->addIdFilter ($ids)
    ->addAttributeToSelect('color')-> groupByAttribute('color');
2
  • Good call, that is even faster. Thank you! Commented Jun 17, 2014 at 9:01
  • 1
    Yep...nice idea with the grouping.
    – Marius
    Commented Jun 17, 2014 at 9:22
3

Seams like you are doing it ok. I think you can also skip ->addFilterByRequiredOptions() but not sure.
Here is an alternative. I don't know if it's better, but you can test and see the time difference.
The idea is to retrieve the child products collection like this:
Let's say that $_product is the configurable product.

$ids = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($_product->getId()); //get the children ids through a simple query
$_subproducts = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('entity_id', $ids)
    ->addAttributeToSelect('color');
4
  • I had to change getting the $ids to $ids = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($_product->getId());. Your way of getting the id's returned NULL in my case. Apart from that, it saves about 20% of the time. Thanks! Commented Jun 17, 2014 at 8:25
  • @SanderKoedood. Oups. I forgot to add something. I changed the answer. But I guess is similar to what you did.
    – Marius
    Commented Jun 17, 2014 at 8:31
  • Your edit gives me the following fatal error: a:5:{i:0;s:66:"SELECT e.* FROM catalog_product_entity` AS e WHERE (((())))` Commented Jun 17, 2014 at 8:57
  • @SanderKoedood. Arrr...ignore it. Just go with what you got. I will change the answer.
    – Marius
    Commented Jun 17, 2014 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.