2

I want to get the image by its label, inside a bundle product's each options selection item. Each selection item is a simple product.

echo Mage::getModel('catalog/product')->load($product_id)
    ->getMediaGalleryImages()->getItemByColumnValue('label', $preview_label_standing)
    ->getUrl();

The file I am calling the above code is from the path app\design\frontend\myCustomPackage\myCustomTheme\template\bundle\catalog\product\view\type\bundle\option\radio.phtml. Here $product_id contains simple product id and $preview_label_standing contains the label i have given for my image from the backend.

P.s. And I want to add one more thing the same code is working in my localhost, but not in server :(

Many thanks in advance.

2

The media gallery gets returned as type Varien_Data_Collection and so you need to use the function getItemsByColumnValue to filter on it. The following code will allows you to filter your media gallery by label.

/** @var Varien_Data_Collection $mediaGallery */
$mediaGallery = Mage::getModel('catalog/product')->load(165)->getMediaGalleryImages();
$filteredMediaGallery = $mediaGallery->getItemsByColumnValue('label', 'test label');
foreach ($filteredMediaGallery as $singleMediaGallery) {
    $mediaUrl = $singleMediaGallery->getUrl());
}

Note: that this will still be a collection and so you will need to loop through the results, or simply get the first item

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.