2

I want show relation information from the products under the descriptions, this information shouldn't be inside a tab but as if it were a dynamic block, for example, each product will have a different author. And I want to show this author. How should do it?

Thanks

3 Answers 3

0
if($product->getCustomAttribute('custom_attribute') !== null) {
    $customAttribute = $product->getCustomAttribute('custom_attribute')->getValue();
    echo $customAttribute;
}
4
  • It dont work for me, I put this code under my tabs and when i update the magento my tabs dissapear and dont show nothing
    – Oriol
    Commented Sep 13, 2018 at 9:43
  • What is the name of your product object ?
    – PЯINCƎ
    Commented Sep 13, 2018 at 9:46
  • it isn't on my file where im working, the object product doesnt exist in my current file
    – Oriol
    Commented Sep 13, 2018 at 10:02
  • ahuuum, in this case you have to do product collection in block module, then you get that collection exemple : public function getCustomProdCollection() in your phtml in the same module like this $block->getCustomprodCollection() and now you do some foreach on it then you will ba able to get your attribute
    – PЯINCƎ
    Commented Sep 13, 2018 at 10:08
0

Try the below code:

if($author = $_product->getAuthor()){
    echo $author;
}

Here attribute code is author if its product_author then it will getProductAuthor()

2
  • It dont work for me, I put this code under my tabs and when i update the magento my tabs dissapear and dont show nothing
    – Oriol
    Commented Sep 13, 2018 at 9:43
  • Where did you add this code? Can you please update your code to your question? Commented Sep 13, 2018 at 10:29
0

you need to do code like this.

<?php 
    $OM = \Magento\Framework\App\ObjectManager::getInstance();
    $product= $OM->get('Magento\Framework\Registry')->registry('current_product');

    if($product->getCustomAttribute('custom_attribute') !== null) {
         $customAttribute = $product->getCustomAttribute('custom_attribute')->getValue();
         echo $customAttribute;
    }

?>
2
  • Where do I have to put that? On view or controller?
    – Oriol
    Commented Oct 9, 2018 at 11:51
  • you need to add this on view. Commented Oct 9, 2018 at 12:59

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.