0

I have overridden the Magento catalog phtml file in our store's custom template to display a link to a custom instructional page that we created.

enter image description here

Problem is, I only want this link to display on certain product pages.

I've noticed that on this page we have access to the variable $_product through the call to

$_product = $block->getProduct();

I've tried logging this object using print_r, etc. but nothing seems to work. How can I see the shape of this object so that I can figure out how to locate something like: $_product->category

Any advice on a better procedure to get this kind of functionality into the catalog?

2
  • have you created custom block also?
    – Abdul
    Dec 8 '17 at 4:17
  • @Abdul curious if that's a better way to go, when would you want to create a custom block for this kind of situation? Dec 8 '17 at 5:13
1

To get all the data of a product you can use following.

$_product = $block->getProduct();
echo '<pre>';
print_r($_product->getData());
echo '</pre>';
die();

To get name of product you can use following for example

$_product->getName();
10
  • I'm not seeing anything though, just an empty code area from the <pre></pre> tags -- does this mean that there is no product data? Dec 8 '17 at 5:19
  • Please check get_class($_product); and let me know what output you get. Dec 8 '17 at 5:27
  • 1
    Ok that means there is data. Can you print $_product->getName(); Dec 8 '17 at 5:55
  • 1
    Very Good answer :)
    – Abdul
    Dec 8 '17 at 6:13
  • 1
    @DanielThompson You get product object now whichever attribute you want you can use by either directly as we get name or by getting $_product->getResource()->getAttribute('Your_Attribute_Code')->getFrontend()->getValue($_product); Dec 8 '17 at 6:19

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.