How do I get product ID and product attribute at magento/module-catalog/block/product/view/gallery.php?

Please help, I need to do some image filtering by product custom attribute with the image caption.


1 Answer 1


You can use getProduct() function directly in this file because that extends below file which contain this function.


You can use below line to get Product ID in that file..

<?php $productId = $this->getProduct()->getId(); ?>

And you can get attribute value using this line

<?php $this->getProduct()->getAttributeText('attribute_code'); ?>

You can replace your attribute code with attribute_code.

Hope this will help you!

  • 1
    Thanks! you are life saver!
    – Paul Fan
    Nov 25, 2019 at 17:12
  • 1
    Can you please upvote? Thanks 😊👍 Nov 26, 2019 at 3:23
  • Is there any other way to get the product custom attribute code? I don't know why it doesn't work on my other page <?php $this->getProduct()->getAttributeText('attribute_code'); ?> or I have to include something? please help
    – Paul Fan
    Nov 27, 2019 at 1:34
  • Where you're using this code ? <?php $this->getProduct()->getAttributeText('attribute_code'); ?> can you please let me know so I can check. Nov 27, 2019 at 3:58
  • I was adding some custom php script inside, I need to get the product custom attribute called "image_type", app/code/vendor/Tools/Block/Product/Bundle/Configurable.php Thanks! I am very apperciate your help.
    – Paul Fan
    Nov 27, 2019 at 4:07

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.