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
    Commented Dec 8, 2017 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? Commented Dec 8, 2017 at 5:13

1 Answer 1

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? Commented Dec 8, 2017 at 5:19
  • Please check get_class($_product); and let me know what output you get. Commented Dec 8, 2017 at 5:27
  • 1
    Ok that means there is data. Can you print $_product->getName(); Commented Dec 8, 2017 at 5:55
  • 1
    Very Good answer :)
    – Abdul
    Commented Dec 8, 2017 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); Commented Dec 8, 2017 at 6:19

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.