Question

I would like add some static text to product views on product-info-main and should be multi language compatible how would I do that? As I understand I can inject static-block content but somehow is not working for me.

I added the following to my custom themes Maganeto_Catalog -> catalog_product_view.xml

   <block class="Magento\Cms\Block\Block" name="custom.text">
        <arguments>
             <argument name="static_text" xsi:type="string">custom</argument>
        </arguments>
   </block>

and created in backed a static block with id static_text but do not show up.

Was it helpful?

Solution

Magento 2 layout mechanism may load and process all the active layouts having the same name. So, in your case, if you want to add more content to product view, you can create the layouts having the same name with the product core layouts. For example, we create a module with the following folder structure:

enter image description here

In catalog_product_view.xml:

   <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="product.info.type">
            <block class="Magento\Cms\Block\Block" name="hello-world">
                <arguments>
                    <argument name="block_id" xsi:type="string">hello-world</argument>
                </arguments>
            </block>
            <block class="Magento\Framework\View\Element\Template"
                   name="boolfly.block.bundle"
                   template="Boolfly_ProductsBlock::hello.phtml"/>
            <block class="Boolfly\ProductsBlock\Block\HelloWorld"
                   name="hello.world" after="-" />
        </referenceContainer>
    </body>
</page>

We added three blocks. The first block will call a static block with id hello-world. The second block is the core block Magento which will render our hello templates. The last one is our custom block to show hello world:

<?php

namespace Boolfly\ProductsBlock\Block;

class HelloWorld extends \Magento\Framework\View\Element\AbstractBlock
{
    protected function _toHtml()
    {
        return '<div>hello world</div>';
    }
}

The same logic for bundle and configurable layout. The most important thing is that we need to reference to a container node of catalog product layout.

There is another way that we will override the layout in our theme. For example, create catalog_product_view.xml under: <Vendor Theme>/<Theme>/Magento_Catalog/layout/catalog_product_view.xml. And then, follow the same logic above to add our block content.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top