Magento - How to link the Configurable Product image to the Simple Product image?
Asked Answered
M

4

10

This is the situation:

I have a configurable product with several simple products. These simple products need to have the same product image as the configurable product. Currently I have to upload the same image to each simple product over and over again.

Is there a way to link the product image of the configurable product to the simple products?

Some of my products have 30 simple products in 1 configurable product and it is overkill/annoying to upload the same image 30 times.

I hope someone can help me with this problem!

Thanks in advance!

Minstrel answered 20/2, 2011 at 23:21 Comment(2)
does each simple product only belong to a single configurable product?Stochmal
Yes, each simple product belongs to a single configurable productMinstrel
S
11

Insert this into your DOCROOT\app\design\frontend\<pachage>\<theme>\template\catalog\product\view\media.phtml after $_product = $this->getProduct();

$_parentIdArray = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($_product->getId());
if(sizeof($_parentIdArray)==1 && Mage::getModel('catalog/product')->load($_parentIdArray[0])->getTypeId() == 'configurable'){
  $_product = Mage::getModel('catalog/product')->load($_parentIdArray[0]);
}

That will use the images belonging to the parent configurable product if the simple product has a single parent of type configurable.

EDIT

To use this in the list view, open DOCROOT\app\design\frontend\<pachage>\<theme>\template\catalog\product\list.phtml and insert the same code block in 2 locations:

  • line 45 after <?php foreach ($_productCollection as $_product): ?> (inside the <?php ?> wrappers)
  • line 106 (approx, might be different in your theme) after <?php $i=0; foreach ($_productCollection as $_product): ?>

Both locations are required to deal with the grid view and list view versions of the page.

HTH,
JD

Stochmal answered 21/2, 2011 at 0:6 Comment(7)
Thanks Jonathan, it works for me on the productpage. Is it possible to make this work in the product list? catalog/product/list.phtml? Thanks in advance!Minstrel
@Jonathan Day - forgot to mention your name in my comment. I hope you can help me out on this one. Thanks in advance!Minstrel
No problem, feel free to click the tick next to my answer if it was the correct solution. That's how StackOverflow works :)Stochmal
@Jonathan Day - What if I only want to show the image of the configurable product in the simple product in the list.phtml? At this moment, the simple products are being replaced by the configurable product and I want the simple products to stay there. These simple products need the same image as the configurable product. Is that possible? Thanks in advance!Minstrel
I'm not sure that I understand your question. Are you saying that you want list.phtml to display each simple product but not the actual configurable product? Normally only the configurable product is shown and the simple products are hidden. If the customer clicks on the simple product, which product page are they taken to?Stochmal
Thanks for you quick response. It's a bit difficult to explain why my simple products are shown, but it's mandatory. The simple products are only visible when a visitor searches for it. The catalog only shows the configurable ones. When the customer clicks on the simple product, they will see the product page of the simple product itself where all configurable options are "filled in". Because every configurable option is filled in, the customer will buy the right product depending on his search terms. I can send you the link to the website if you would like that.Minstrel
Hi Niels, that's possible, but outside the scope of your original question. I suggest you accept this answer if it answered your original question and ask a new one. I'll answer that when I get a free moment!Stochmal
L
4

I believe that the right way to do this is to override the image helper (app/core/Mage/Catalog/Helper/Image.php), so that in the init function, you check if you have a simple product, and if you do, replace that with the configurable product. This should effect the change for ALL templates.

Latrinalatrine answered 23/3, 2011 at 19:55 Comment(2)
definitely, your approach is the best, because in this case we need to change logic only in one place. But this may cause performance issue, so it' needed to implement some caching for parent productsLustrate
yeah, well, it seems to be that magento is one big performance issue no matter what you do :)Latrinalatrine
D
2

A quick workaround might be to export your product list (Admin > System > Import/Export > Profiles), put the image file name in the appropriate column(s) for all your simple products, copy the file(s) to media/import/ directory then import the modified product list. The various associations will be made for you and the image file(s) will be copied to wherever they need to be.

Deciduous answered 20/2, 2011 at 23:36 Comment(2)
this will duplicate a lot of imagesMonosymmetric
Thanks for the answer, but I'm using Jonathan's solution for the product view page. I hope he can tweak the script for the product list page.Minstrel
L
2

I think the best way is to override catalog image helper (like @stew said). To avoid performance issues we can use raw sql queries to fetch parent's image value:

class Wfs_Catalog_Helper_Image extends Mage_Catalog_Helper_Image
{
    public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
    {
        parent::init($product, $attributeName, $imageFile);
        if (!$product->getId() || $imageFile || $product->isConfigurable()) {
            return $this;
        }

        $productImage = $product->getData($attributeName);
        if (!$productImage || $productImage == 'no_selection') {
            // Get parent product's attribute
            $value = $this->getParentProductAttribute($product->getId(), $attributeName);
            if ($value) {
                $this->_getModel()->setBaseFile($value);
            }
        }
        return $this;
    }

    public function getParentProductAttribute($productId, $attributeName)
    {
        $coreResource = Mage::getSingleton('core/resource');
        $conn = $coreResource->getConnection('core_read');

        $attrId = Mage::getSingleton('eav/config')
            ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeName)
            ->getId();

        $select = $conn->select()
            ->from(array('rel'  => $coreResource->getTableName('catalog/product_relation')), array())
            ->join(
                array('var' => $coreResource->getTableName('catalog_product_entity_varchar')),
                'var.entity_id = rel.parent_id',
                array('value')
            )
            ->where('rel.child_id = ?', $productId)
            ->where('var.attribute_id = ?', $attrId);

        return $conn->fetchOne($select);
    }
}
Lustrate answered 11/5, 2012 at 15:47 Comment(3)
@gonzela2006 you can create you own module, and override image helper in it.Lustrate
Thank you very much. Can I ask you please to check this question #19932320 pleaseLamothe
This isn't working on Magento 1.8 I presume that's why @gonzela2006 asked a new question.Antagonistic

© 2022 - 2024 — McMap. All rights reserved.