• Eyelash / php jquery image management
  • Magento Configurable products : change product image on thumbnail option

    This article describes a  color chooser for magento product page, a technique that automatically use product attributes from a Magento admin to display color thumbnails that

    • allows end user to choose color option by clicking on thumbnail
    • change product image to associated product image

    the technique above is inspired from Magento Wiki page that describes the javascript that catches the Options price select event to modifyu the product image. We’ve changed a few things though

    1. this example is a color chooser. First thing is to create a folder in media/productColors with thumbnails of available colors, all stored in a file with the name of the color
    2. follow the Magento Wiki tutorial carefully
    3. instead of adding the code to view.phtml, create a new phtml file
      app/design/frontend/*/*/template/catalog/product/view/WebmasterBulletin_JsConfigColors.phtml with the following code


      <?
          $_helper = $this->helper('catalog/output');
          $_product = $this->getProduct();
      ?>
      <script type="text/javascript">
          var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>);
      
          var assocIMG = { // Added
          <?php
          if ($_product->getTypeId() == "configurable") {
              $associated_products = $_product->loadByAttribute('sku', $_product->getSku())->getTypeInstance()->getUsedProducts();
      
              foreach ($associated_products as $assoc)
                  {$dados[] = $assoc->getId().":'".($assoc->image == "no_selection" || $assoc->image == "" ? $this->helper('catalog/image')->init($_product, 'image', $_product->image)->resize(365,400) : $this->helper('catalog/image')->init($assoc, 'image', $assoc->image)->resize(365,400))."'";
                  }
          } else {
              $dados[] =  "''";
          }
          echo implode(',', $dados );
          ?>
          }   
      
      function displayProductConfigImage(id)
      {
      jQuery('#image0').attr('src', assocIMG[id]) ;
      jQuery('#image0').attr('jqimg', assocIMG[id]) ;
      jSelectImage('0');
      }
      function selectColor(idAttribute, idProduct)
      {
      jQuery('#attribute76').val(idAttribute);
      spConfig.reloadPrice();
      displayProductConfigImage(idProduct);
      }
      var tipConfigImgThumb = "Choose your color :";
      </script>
      <?  $ii=0;foreach ($associated_products as $assoc)
               {
               $color=$assoc->getAttributeText("color");
               $w=100; $h=100;
               $imageColorSource =  "/media/productColors/" . strtolower($color) . ".jpg";
               $imageColorResized =  Mage::getBaseUrl() . "/media/phpThumb_1.7.9/phpThumb.php?src=" .  $imageColorSource . "&w=$w&h=$h&iar=1";?>
               <a href="javascript:selectColor(<?=$assoc->getColor()?>, <?=$assoc->getId()?>)" id="ConfigImgThumb<?=$assoc->getId()?>"><img src="<?=$imageColorResized?>" width="<?=$w?>" height="<?=$h?>" alt="<?=$color?>" border="0"></a>
      <script>new Tip('ConfigImgThumb<?=$assoc->getId()?>', tipConfigImgThumb + '<b><?=$color?></b>');</script><?
               }
           ?>
    4. reference WebmasterBulletin_JsConfigColors.phtml in layout/catalog.xml, in the catalog/product_view block section :
          <block type="catalog/product_view" name="product.products_jsConfigColors" as="webmasterbulletin_products_js_config_colors" template="catalog/product/view/WebmasterBulletin_JsConfigColors.phtml" />
    5. in the product.view.phtml you can now place your color chooser where you like
      <?if ($_product->isSaleable() && $this->hasOptions()) echo $this->getChildHtml('webmasterbulletin_products_js_config_colors');?>
    1. 8 Responses to “Magento Configurable products : change product image on thumbnail option”

    2. August 31, 2010 - Johannes

      Hi,
      I don’t know what I have done wrong, but I have still the dropdown instead of the thumbnails.

      Do you know the misstake?

      Thanks in advance.

      Johannes

    3. September 28, 2010 - chonkton

      Does this work for 1.4?

      It seems other solutions like this are for earlier versions.

      Many thanks.

    4. October 12, 2010 - Rob

      This is great and exactly what I am looking for! except it will not change the image when thumbnails are clicked.. Do you happen to know why?

    5. October 14, 2010 - Mark

      Hi, Is the person who wrote this handy tutorial available to help me out?

      I’ve followed the steps listed above and have also followed the wiki but I’m not getting it working as it should. I see no difference at all on the frontend – but I’m not quite sure what I should be doing with the configurable products in the admin.

      Many thanks

    6. October 20, 2010 - jacopo

      hi.
      do you have a demo for this? i am not sure 100% of what is this for, but sounds interesting…

    7. October 23, 2010 - jacopo

      I am sorry for the previous comment, I read your article more carefully and it’s in fact clear what is this for.

      I have another related question thou:
      your sample (as some others around) starts by having color options displayed as Color Thumbnails.
      Instead I would like as color option not a default thumbnail, but a view of the related product.

      display
      Is there any example

    8. December 21, 2010 - Jonas

      It’s works, but needed some corections

    9. April 28, 2011 - nico

      Hi,

      this part : http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/change_product_image_on_view_page_to_associated_product_s_image

      dosen’t work anymore after upgrade from Magento 1.4.2 to 1.5.1 do you eventually know how to make it work in the new Magento Version.

    Quick contact form