2

Currently I'm working on a Magento 2.1 webshop which deals in car parts. Each part should should a list of car models that can use this part. This list is generated dynamically based on a set of filters attached to the products.

Generating the list items is no problem, but now I want to show these items in a list below the description. The content has to be appended to the contents of the product.info.description block.


So far I've managed to add the list as a separate tab using the following code:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.details">
            <block class="MyModule\CarLabels"
                   name="carlabels.tab"
                   as="carlabels"
                   template="MyModule::carlabels.phtml"
                   group="detailed_info">
            </block>
        </referenceBlock>
    </body>
</page>

Since I don't want an extra tab, I've also tried to add the block after the description block. The code below did not work:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.details">
            <block class="MyModule\CarLabels"
                   name="product.info.carlabels"
                   template="MyModule::carlabels.phtml"
                   after="product.info.description">
            </block>
        </referenceBlock>
    </body>
</page>

In /vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml you can see that the description block calls a method called getDescription(), so I'm thinking one might be able to overwrite this method and append to it without destroying the original content?

Reference (taken from Magento source code):

<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.details" template="product/view/details.phtml" after="product.info.media">
    <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.description" template="product/view/attribute.phtml" group="detailed_info">
        <arguments>
            <argument name="at_call" xsi:type="string">getDescription</argument>
            <argument name="at_code" xsi:type="string">description</argument>
            <argument name="css_class" xsi:type="string">description</argument>
            <argument name="at_label" xsi:type="string">none</argument>
            <argument name="title" translate="true" xsi:type="string">Details</argument>
        </arguments>
    </block>
    <block class="Magento\Catalog\Block\Product\View\Attributes" name="product.attributes" as="additional" template="product/view/attributes.phtml" group="detailed_info">
        <arguments>
            <argument translate="true" name="title" xsi:type="string">More Information</argument>
        </arguments>
    </block>
</block>

I'm hoping someone can point me in the right direction here. Does anyone know how to append to the product description?

PS In case you didn't notice yet, I'm a Magento-newb. Googling this issue always seems to lead back to articles about creating a new tab, which is not the goal in this case (and something that I already created as a working test case).

1 Answer 1

2

Hopefully you found an answer to your issue as it has been over 3 months but as I was having the same issue and stumbled upon this ticket I thought I'd mention my fix.

As you mention, trying to update the layout to include your own block, was not working.

What I had to do was create a plugin and append to the _toHtml() function:

di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

<type name="\Magento\Catalog\Block\Product\View\Attributes">
    <plugin name="Name_Goes_Here"
            type="File\Dir\Plugin\AttributesOutputPlugin"/>
</type>

</config>

Then create the Plugin file File\Dir\Plugin\AttributesOutputPlugin.php:

<?php

use Magento\Catalog\Block\Product\View\Attributes;

class AttributesOutputPlugin
{
/** @var View */
private $productPdfView;

public function __construct(YourBlock $blockName)
{
    $this->blockName = $blockName;
}

public function afterToHtml(Attributes $subject, string $html)
{
    $htmlAppend = $this->blockName->generateHtmlToAppend();
    return $html . $htmlAppend;
}

Hopefully this might help.

Regards, Mark

1
  • Thank you for your answer! Unfortunately, I am no longer in the position to check this answer, as we've moved on to other solutions. I hope someone else will find this thread and use it to their advantage. Apr 14, 2017 at 11:18

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.