0

I'm helping a friend out with some REALLY BAD magento code (which placed in a custom-made template).

In a category view I have the following code which should get simple products of a configurable product and query the simple products.

I'm not a big magento expert, but from reading, I have a feeling that looping and loading each product is a bit over-kill.

Any suggestions of how to improve the code and to read about best practices abotu querying and getting only needed data(in this case is simple products images.)

CODE:

$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
foreach ($_productCollection as $_product):
<div class="item-product-wrap">    
          <?php
          // Get full product object.
          $conf_product = Mage::getModel('catalog/product')->load($_product->getId());

          // Get all simple products.
          $simple_products = $conf_product->getTypeInstance(true)->getUsedProducts(null, $conf_product);

          // Get default simple product ID.
          $default_simple_product_id = $conf_product->getData('default_configuration_id');
          // If default simple product is not set, get the first one.
          $default_simple_product_id = !$default_simple_product_id ? $simple_products[0]->getId() : $default_simple_product_id;

          // Run through all simple products.
          foreach ($simple_products as $simple_product):
            $simple_product_loaded = Mage::getModel('catalog/product')->load($simple_product->getId());

            if ($simple_product_loaded->getStatus() == 2) {
              // Product is disabled.
              continue;
            }

            // Get color. Replace spaces and ampersands with dashes to support URLs.
            $color = str_replace('&', '-', str_replace(' ', '-', $simple_product->getAttributeText('color')));

            // Get image.
            $product_media_config_model = Mage::getModel('catalog/product_media_config');
            $small_image = $product_media_config_model->getMediaUrl($simple_product->getImage());
            ?>
<div class="one-simple-product<?php echo ($default_simple_product_id == $simple_product->getId() ? ' active' : '') ?>">

    <a href="<?php echo $_product->getProductUrl() . '#' . $color ?>" title="<?php echo $this->stripTags($this->getImageLabel($simple_product, 'small_image'), null, true) ?>" class="product-image">
    <img src="<?php echo $small_image; ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($simple_product, 'small_image'), null, true) ?>" />
    </a>

</div>

2 Answers 2

3

You can use the following:

public function getChildrenCollection() {

    $parent = Mage::registry('current_product');
    $parentId = $parent->getId();

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

     $childrenCollection = Mage::getModel('catalog/product')->getCollection()
        ->addIdFilter($childrenIds)
        ->addAttributeToSelect(array_of_attributes_you_want_to_load);

     return $childrenCollection;
}

Remember to put this code in a Block (there shouldn't be any PHP in template files, you can just call $this->getChildrenCollection()) and don't use * in the attribute to select, only specify the attributes you really need

0

Following extension will be useful to you to display only selected part of simple products (associated products) of configurable product.

You may show/hide simple product's attribute like price, image, name, & description etc.

https://github.com/organicinternet/magento-configurable-simple

Your Answer

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

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