0

I am trying to include a product image that has a specific custom role (in the example below it's hover_img)

The file I am doing this on is:

vendor/magento/module-page-builder/view/frontend/templates/catalog/product/widget/content/carousel.phtml

I am trying to display it by doing this:

<?php
$customImg = $_item->getResource()->getAttribute('hover_img')->getFrontend()->getValue($_item);
?>

<?= $customImg ?>

But nothing is coming back it's just empty, wondering if anyone knows the correct way.

How can do this anyone have an idea?

(The end result of this is that when hovering over a product image in the carousel the hover image of that product is shown but I am struggling to pull in the image with a custom role)

1 Answer 1

0

Here are the two examples could you please try.

First:

<?php
$customImg = $_item->getData('hover_img'); // Assuming 'hover_img' is a custom attribute code

if ($customImg) {
    $imageUrl = $this->helper('Magento\Catalog\Helper\Image')
        ->init($_item, 'hover_img') // Replace 'hover_img' with your custom attribute code
        ->setImageFile($customImg)
        ->getUrl();
    
    echo '<img src="' . $imageUrl . '" alt="' . $_item->getName() . '" />';
} else {
    echo 'No custom image available'; // Display a message if the custom image is not set
}
?>

Second:

<?php
/** @var \Magento\Catalog\Model\Product $_item */

// Get the product's custom image URL
$customImage = $_item->getData('hover_img');

if ($customImage) {
    // Generate the image URL based on the custom image path
    $imagePath = 'pub/media/catalog/product' . $customImage;
    $customImageUrl = $block->getUrl($imagePath);

    // Display the custom image
    echo '<img src="' . $customImageUrl . '" alt="' . $_item->getName() . '" />';
} else {
    // If there is no custom image, display the default product image
    echo $block->getImage($_item, 'category_page_list')->toHtml();
}
?>

Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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