0

I'm selling eyewear and am currently running Magento 1.9.0.1 (still in development). Each SKU is the frame. My category hierarchy is: Men -> Eyewear -> Sunglasses Women -> Eyewear -> Sunglasses

It's possible that a frame belongs to all the above categories. For certain frames, I have photos of that frames with sunglass lenses and some with regular eyewear lenses. I want to only show the sunglass lens photos on the product page if the item was selected under the sunglass category, and only show the optical lens photos if the item was found under the eyewear category.

As an example, if the user navigates to Women -> Sunglasses and then clicks a picture of awesome-frame-1 with sunglass lenses, they'll go to http://www.myeyewearsite.com/women/sunglasses/awesome-frame1.html and I want the default product image on the product page to be a photo of awesome-frame1 with sunglass lenses, the same image they clicked on from the catalog page to get there. Conversely, if the user navigates to Women -> Eyewear and then clicks a picture of awesome-frame-1 with eyewear (non-sun) lenses, they'll go to http://www.myeyewearsite.com/women/eyewear/awesome-frame1.html and I want the default product image on the product page to be a photo of awesome-frame1 with eyewear (non-sun) lenses, the same image they clicked on from the catalog page.

If you're wondering why I don't just use a different SKU for sun vs eyewear, it's because we are working with a 3rd party that is charging us based on the number of SKUs. So, we're trying to keep them to a minimum.

Is there a way to present only a subset of product images based on its category? I've spent hours playing around in my CMS and haven't found a way to do it through there. I was unable to find an answer looking at other questions asked in this forum either, so any guidance would be appreciated.

1

This is a very broad subject. I doubt you will get a fully working solution.
But I can give you some tips on how I would do it.
First I would add multiple image attributes for products. As many as you need to cover your categories. Here is a tutorial on how to do that.
Then I would add a category attribute. A dropdown with a custom source model. This dropdown should contain all the image attributes for the products.

Here is a tutorial that shows how to add an attribute with a custom source model. The tutorial is for products, but it works the same for categories.
And here is a way to retrieve the image attributes for products

$options = array();
$collection = Mage::getResourceModel('catalog/product_attribute_collection')
            ->addVisibleFilter()
            ->addFieldToFilter('frontend_input', 'media_image');
foreach ($collection as $attribute) {
    /** @var Mage_Catalog_Model_Resource_Eav_Attribute $attribute */
    $options[$attribute->getAttributeCode()] = $attribute->getFrontendLabel();
} 

If you put all of these pieces together you should be able to select in the category edit form the product image attribute to show in the product list.

Now you need to modify the product list template in order to use the selected attribute.

You need to change this

<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />

to this: (let's say the category attribute you added has the code product_image).

<?php $category = Mage::registry('current_category');?>
<?php if ($category && $category->getProductImage()) : ?>
    <?php $productImage = $category->getProductImage();?>
<?php else : ?>
    <?php $productImage = 'small_image';?>
<?php endif;?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, $productImage)->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, $productImage), null, true) ?>" />

This should solve the list image.
Now for the product image, you need to do the same inside the media.phtml template

you need to replace this

$_img = '<img id="image" src="'.$this->helper('catalog/image')->init($_product, 'image').'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" />';

with

<?php $category = Mage::registry('current_category');?>
<?php if ($category && $category->getProductImage()) : ?>
    <?php $productImage = $category->getProductImage();?>
<?php else : ?>
    <?php $productImage = 'image';?>
<?php endif;?>
$_img = '<img id="image" src="'.$this->helper('catalog/image')->init($_product, $productImage).'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" />';

you may also need to replace the if statements

so if ($_product->getImage()) becomes if ($_product->getData($productImage)) and so on.

That's all I can think of right now. If I find something else I will let you know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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