1

How to display custom text from attribute after product title on product page Currently, I have created a custom .phtml file and displayed text before the product title:

catalog_product_view.xml

<referenceContainer name="product.col.info">
    <block class="Magento\Catalog\Block\Product\View" name="product.before.title" template="Magento_Catalog::product/view/before_title.phtml" before="-"/>
</referenceContainer>

before_title.phtml

<?php

/**
 * Product view template
 *
 * @var $block \Magento\Catalog\Block\Product\View
 */

$_product = $block->getProduct();
$custom = $_product->getData('brand');

?>

<!--<//?= $custom ?> -->
<div class="brand-name-title"> <?php 
            $optionText = '';
            $attr = $_product->getResource()->getAttribute('brand');

            if ($attr->usesSource()) {
               $optionText = $attr->getSource()->getOptionText($_product->getBrand());
            }
            ?>
            <?php if($optionText){ ?>
                 <?php echo "" . $optionText . "<br>"; ?>
            <?php } ?></div>

This is working correct: enter image description here

Now I am trying to display custom text from attribute subname after the title using another phtml template file (below the title).

after_title.phtml

<?php

/**
 * Product view template
 *
 * @var $block \Magento\Catalog\Block\Product\View
 */

$_product = $block->getProduct();
$custom = $_product->getData('subname');

?>

<!--<//?= $custom ?> -->
<div class="subname-name"> <?php 
            $optionText = '';
            $attr = $_product->getResource()->getAttribute('subname');

            if ($attr->usesSource()) {
               $optionText = $attr->getSource()->getOptionText($_product->getSubname());
            }
            ?>
            <?php if($optionText){ ?>
                 <?php echo "" . $optionText . "<br>"; ?>
            <?php } ?></div>

catalog_product_view.xml

<referenceContainer name="product.col.info">
    <block class="Magento\Catalog\Block\Product\View" name="product.after.title" template="Magento_Catalog::product/view/after_title.phtml" before="-"/>
</referenceContainer>

But here in this case I didn't get the text displayed after the title on product pages.

1 Answer 1

1

Call your template in XML layout file before </body> tag as below.

File: catalog_product_view.xml

<body>
    ...
    <referenceContainer name="content.top.most">
        <container name="custom.block.subname" htmlTag="div" htmlClass="custom-product-subname" before="-">
            <block class="Magento\Catalog\Block\Product\View" name="custom_subname" template="Magento_Catalog::product/view/after_title.phtml" />
        </container>
    </referenceContainer>
</body>

I have tested this and got the content below the product title on product pages.

Hope this will help you!

===== UPDATE =====

Add below code after your <body> tag in your XML layout file. To move your after_title.phtml template file content after product title.

catalog_product_view.xml

<body>
    <move element="custom.block.subname" destination="product.info.main" after="page.main.title"/>
    ...
</body>
5
  • I dont know why, but I also try in your way but impossible display this. You can check in: nansy.pl/alien.html also you can check container nansy.pl/alien.html?ath=1 with shift in console.
    – Sylvester
    Dec 3, 2021 at 11:37
  • I've display 2 times "brand" above title and under title from before_title and after_title but this after title no dsiplayed. I also try only echo test
    – Sylvester
    Dec 3, 2021 at 11:38
  • Your template file is called on the product page. I can see your echo test before the title. SS: nimb.ws/MUmVcy Dec 3, 2021 at 11:58
  • Please check my updates in the above answer. I have added the code to move your template file content below the product title. Dec 3, 2021 at 12:04
  • Thanks for help. Now I resolve this in this way <referenceContainer name="box.inner1"> <block class="Magento\Catalog\Block\Product\View" name="product.after.title" template="Magento_Catalog::product/view/after_title.phtml" before="-"/> </referenceContainer> with some css custom code
    – Sylvester
    Dec 3, 2021 at 12:20

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.