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);

but it seems not working please help what i am missing

  • Hello i cannot comment but looking at your question your role is tile_image and you are trying to get the attribute tiles_images. Is it just that you are mispelling your attribute code here? – Carlo Ricci Oct 28 '19 at 15:53
  • no i am using correct spelling some mistake in posting question – user1799722 Oct 29 '19 at 17:56
  • @user1799722 Have you got the solution? – aravind Oct 30 '19 at 4:34
  • @user1799722 where do you want to show that image? – aravind Oct 31 '19 at 8:37

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.


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)
|improve this answer|||||
  • it is image attribute : prnt.sc/pqhav8 – user1799722 Oct 31 '19 at 7:37
  • 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 – Rakesh Varma Oct 31 '19 at 7:38
  • check the updated answer – Rakesh Varma Oct 31 '19 at 7:47
  • thanks it worked – user1799722 Oct 31 '19 at 10:57
  • You most welcome,,, – Rakesh Varma Oct 31 '19 at 11:22

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')

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

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

<image id="product_tiles_image" type="tiles_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 : ''
|improve this answer|||||
  • where to do this file name for "Create an attribute like this in a data patch" – user1799722 Oct 30 '19 at 1:00
  • app/code/Your/Module/Setup/Patch/Data/AddImageAttribute.php, check in core for example vendor/magento/module-catalog/Setup/Patch/Data/*. – countid Oct 30 '19 at 1:03
  • See my updated reply. – countid Oct 30 '19 at 1:09

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.