Adding Multipule Attribute Images Print

  • 0

Although Zen Cart supports defining an image for a product’s attribute (i.e. different colors of t-shirts for a color attribute), once you add the product to your cart, only the default image will display.  Although you could hack the shopping cart page as per  instructions in the Zen Cart forum, a simpler approach is to edit the shopping_cart class.  This approach may be better as it will cause Zen Cart to output the attribute image anywhere that the shopping_cart class is used.

It is important that all Zen Cart stores that use attribute images implement one of these processes as it can be confusing to customers when they see a different product in their shopping cart than what they selected. The following implementation will resolve this issue.

To begin, open includes/class/shopping_cart.php and find:

$products_array[] = array('id' => $products_id,

Add before:

        // support attribute images
        if (isset($this->contents[$products_id]['attributes'])) {
          foreach ($this->contents[$products_id]['attributes'] as $options_id => $options_values_id) {
            // get the attributes image using the options_id and options_values_id
            $attributes_image = $db->Execute("SELECT attributes_image 
                                              FROM " . TABLE_PRODUCTS_ATTRIBUTES . " 
                                              WHERE products_id = " . (int)$products_id . "
                                              AND options_id = " . $options_id . "
                                              AND options_values_id = " . $options_values_id . "
                                              LIMIT 1;");
            if ($attributes_image->RecordCount() > 0 && $attributes_image->fields['attributes_image'] != '') {
              $products->fields['products_image'] = $attributes_image->fields['attributes_image'];
              break;
            }  
          }
        }

Now, when you visit your shopping cart page, assuming it uses the shopping_cart class, you will now see the image of the attribute that was added. There is one shortcoming, which more relates to Zen Cart itself than this method, and that is that if you have multiple attributes for a product and different images assigned to each attribute, it isn’t possible to assign an image to a combination of attributes (i.e. a large red t-shirt vs. a small red t-shirt). The above code will use the image from the first attribute that it finds (i.e. a red t-shirt, regardless of size).

 


Was this answer helpful?

« Back