Reputation: 65
Is there any way through which simple product image will be used by the configurable product.For example, I have one configurable product of which there is 4 simple product with different images and inventory.So, the Configurable product should pick one of the simple product images which are in stock.If that simple product goes out of stock, then the configurable product should pick the different simple product image automatically which is in stock. I need this functionality because sometimes the colour option shown in a configurable product is not available for the customer.
Devs, Either suggest me some plugin or if it is possible by any code or by any magento configuration or some kind of changes in the product.Also, let me know Is this possible or not. Every suggestion is valuable for me thanks.
Upvotes: 2
Views: 4175
Reputation: 76
Configurable's Base Image in Product View
The accepted answer is a working solution, however, I'd prefer calling getUsedProductCollection() directly as I believe this to be more efficient. getUsedProductIds() eventually calls getUsedProductCollection(), adding multiple attributes while doing so, loops through the collection, picking out and returning the simple product ids. So in the end this means we take multiple loops where we'd only need one, plus loading more stuff than we need.
So, lets assume we're using a copy of Magentos catalog/product/view/media.phtml template. We'd replace lines 36 & 37 with this:
$baseImage = $_product->getImage();
if( $_product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE ){
$simpleCollection = $_product->getTypeInstance()->getUsedProductCollection($_product)
->addAttributeToSelect('image')
// Optionally:
//->addAttributeToSelect('small_image')
//->addAttributeToSelect('thumbnail')
->addFieldToFilter('image', array('notnull'=>true))
->addFilterByRequiredOptions();
// Filter out all child products that are out of stock
Mage::getSingleton('cataloginventory/stock')
->addInStockFilterToCollection($simpleCollection);
if($simpleCollection->getSize()))
$baseImage = $simpleCollection->getFirstItem()->getImage();
}
?>
<?php if ($baseImage != 'no_selection' && $baseImage): ?>
This way, we also have a fallback in case none of the available simple products actually have their own 'Base Image'.
Then in the original line 40 we only have to add $baseImage as the third parameter of Mage_Catalog_Helper_Image::init():
[...]$this->helper('catalog/image')->init($_product, 'image', $baseImage)[...]
And optionally, we could use the same $simpleCollection to list all simple product images as thumbnails, if we liked that:
<?php foreach ($simpleCollection as $_simple): ?>
<li>
<img src="<?php echo $this->helper('catalog/image')->init($_simple, 'thumbnail', $_simple->getImage())->resize(56); ?>" width="56" height="56" />
</li>
<?php endforeach; ?>
Configurable's Small Image in Product Listings
When I first read the question I thought it was asking for displaying the next available child product's 'Small Image' in product listings. Since the question is actually not clear on that, adding at least a short answer to this possible interpretation might be helpful for someone who's, well, looking for exactly that.
In Product Listings we would rather not want to load each configurable's simple collection just to get to one product image. When I was asked to prepare a possible solution I chose to create a new product attribute for configurable (and blunde) products that stores the ID of one of its available child products. I added stock event observers so this value updates whenever the linked child product goes out of stock.
Since we already have an accepted answer I don't think it makes sense to elaborate on this. However, for everyone who's interested in this draft, I've uploaded it here.
Upvotes: 1
Reputation: 1
Check this Product Image Attribute extension, may be it will be useful for you. The extension allows to set media additional attribute. You can point some image as additional attribute to a product.
Press "Ask about" to discuss additional features that you need.
Upvotes: -1
Reputation: 2325
Use below code on media.phtml file (app\design\frontend\{{template name}}\default\template\catalog\product\view\media.phtml)
<?php if($_product->getTypeId() == "configurable"){
$_configurable = $_product->getTypeInstance()->getUsedProductIds();
foreach ($_configurable as $_config){
$_simpleproduct = Mage::getModel('catalog/product')->load($_config);
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_simpleproduct);
if($stock->getQty() && $stock->getIs_in_stock()){
foreach ($_simpleproduct->getMediaGalleryImages() as $image) {
echo '<img src="'.$image->getUrl().'" /><br>';
}
break;
}
}} ?>
Upvotes: 2