0

In Magento 2.4.3 I have created a page builder product attribute labeled "Before Content" which the admin will populate with html and I want to show in product page. Under child theme I have created Magento_Catalog/templates/product/view/before-content.phtml

<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();
if ($_attributeLabel && $_attributeLabel == 'custom') {
    $_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
$_attributeValue = $_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
?>

<?php if ($_attributeValue): ?>
    <div class="before-content" <?php  echo $_attributeAddAttribute;?>>
        <?php echo $_attributeValue; ?>
    </div>
<?php endif; ?>

In the catalog_product_view.xml I have added the following:

<move element="before-content" destination="info_wrap" before="product.info.details"/><referenceBlock name="product.info.main">
<block class="Magento\Catalog\Block\Product\View\Description" name="before-content" template="Magento_Catalog::product/view/before-content.phtml" group="detailed_info">
    <arguments>
        <argument name="at_call" xsi:type="string">getBeforeContent</argument>
        <argument name="at_code" xsi:type="string">before_content</argument>
        <argument name="css_class" xsi:type="string">before-content</argument>
        <argument name="at_label" xsi:type="string">before_content</argument>
        <argument name="add_attribute" xsi:type="string">itemprop="before_content"</argument>
        <argument name="title" translate="true" xsi:type="string">Before content</argument>
    </arguments>
</block>
</referenceBlock>

Using the following variable in before-content.html returns "escaped" (plain text) result and not rendered html.

$_attributeValue = $_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);

How can I render the html in product page?

2

1 Answer 1

0

Try this:

<?php echo $this->helper('Magento\Catalog\Helper\Output')->productAttribute($_product, $_product->get@attribute_code(), '@attribute_code'); ?>

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.