Reputation: 103
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!
Upvotes: 10
Views: 10092
Reputation: 3655
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);
}
}
Upvotes: 2
Reputation: 11366
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.
Upvotes: 4
Reputation: 18702
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:
<?php foreach ($_productCollection as $_product): ?>
(inside the <?php ?>
wrappers)<?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
Upvotes: 11
Reputation: 37700
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.
Upvotes: 2