Question

i have created new image role name "tiles_images", i product listing page i want to show tile_image only , for this i have done below code but that seems not working

i am using below code in my Magento_Catalog/templates/product/list.phtml

$customIcon = $_product->getResource()->getAttribute('tiles_images')->getFrontend()->getValue($_product);
print_r($customIcon);

but it seems not working please help what i am missing

Was it helpful?

Solution

I have faced the same issue if you have created attribute same way as @AliHussain mention then you have to just need to change one thing in attribute management using inspect element change the value of used_in_product_listing attribute value to yes

another way is if you have not set value for your attribute in all products then delete that attribute and while creating attribute before selecting attribute type just go to the Storefront Properties and select Used in Product Listing to Yes and then got to Properties tab and add required details.

screenshot of Storefront Proerties tab

Hope this will fix your issue.

UPDATE

Not Recommended

use below query to change the value.

UPDATE catalog_eav_attribute SET used_in_product_listing=1 where attribute_id=(SELECT attribute_id FROM eav_attribute WHERE `attribute_code`='tiles_images' and `entity_type_id` = 4)

OTHER TIPS

Since I cannot add Comment. You have called the right way, that's how we get value of custom attributes. Can you show what you have selected here and can you just simply show configuration of your attribute from here.

Product Attribute Creation From Admin

Considering you have a media image can you try this and check:

$productImageAttr = $_product->getCustomAttribute( 'tiles_images' );
$productImage = $this->helper('Magento\Catalog\Helper\Image')
->init($_product, 'tiles_images')
->setImageFile($productImageAttr->getValue());

<img src="<?php echo $productImage->getUrl() ?>" alt="<?php echo $block->escapeHtml($_product->getTitle()) ?>" />

In theme's view.xml file declare your image attribute:

<image id="product_tiles_image" type="tiles_image">
    <width>338</width>
    <height>338</height>
</image>

Create an attribute like this in a data patch:

namespace Your\Module\Setup\Patch\Data;

use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class AddImageAttribute implements DataPatchInterface
{
    /** @var ModuleDataSetupInterface */
    private $moduleDataSetup;

    /** @var EavSetupFactory */
    private $eavSetupFactory;

    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, 'tiles_image', [
            'type' => 'varchar',
            'label' => 'Tiles Images',
            'input' => 'media_image',
            'frontend' => 'Magento\Catalog\Model\Product\Attribute\Frontend\Image',
            'required' => false,
            'global' => ScopedAttributeInterface::SCOPE_STORE,
            'used_in_product_listing' => true,
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

Then on product page images tagged with tiles_image you can load like this in product/list.phtml:

$image = $block->getImage($_product, 'product_tiles_image');
$tilesImageHtml = null;
if (!empty($_product->getData('tiles_image'))) {
    $tilesImageHtml = $image->toHtml();
}
echo $tilesImageHtml ? $tilesImageHtml : ''
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top