0

I would like to add a CMS block to the bottom of each product list whehter it is on a category page or the search result page. Since I would like to add it on every list I'll try to do this via layout customization within default.xml of my theme.

Unfortunatelly I'm not able to reference on the related container or block.

I'm able to add a block to the content container by using following code:

<referenceContainer name="content">
    <block class="Magento\Cms\Block\Block" name="block_category_below_collection">
        <arguments>
            <argument name="block_id" xsi:type="string">block_category_below_collection</argument>
        </arguments>
    </block>
</referenceContainer>

But this show the cms_block on every page at the top, not only at th bottom of product lists. I tried other reference names like:

<referenceContainer name="category.products.list">
<referenceBlock name="category.products.list">

But I'm not sure which is the correct one and I'm not able to determine it by my self. Enabling template and block hints states that the template file is /var/www/html/px.local/mage22/vendor/magento/module-catalog/view/frontend/templates/category/products.phtml and the related block is "Magento\Catalog\Block\Category\View".

But here I'm stuck.

Can anyone help to determine how to add the CMS block?

  • try to use after="-". So your block tag <block class="Magento\Cms\Block\Block" name="block_category_below_collection" after="-"> – user55548 Mar 27 at 11:43
  • Yes that fixed, that the CMS block is rendered at the top. Thank you. But my main issue is that I'll try to reference only to category product list and search result product list. In my example above I'm referancing to the content container which renders the CMS block to each page, including each CMS page. – bahaspletma Mar 27 at 12:59
1

Instead of:

<block class="Magento\Cms\Block\Block" name="block_category_below_collection">
    <arguments>
        <argument name="block_id" xsi:type="string">block_category_below_collection</argument>
    </arguments>
</block>

Try:

<block class="Magento\Cms\Block\Block" name="block_category_below_collection" after="-">
    <arguments>
        <argument name="block_id" xsi:type="string">block_category_below_collection</argument>
    </arguments>
</block>

Note the after="-" difference.

Update: One more thing to notice is that you have used default.xml for the layout changes, which will add the layout update in all pages.

Please use it in catalog_category_view.xml and catalogsearch_result_index.xml.

  • Yes that fixed, that the CMS block is rendered at the top. Thank you. But my main issue is that I'll try to reference only to category product list and search result product list. In my example above I'm referancing to the content container which renders the CMS block to each page, including each CMS page. – bahaspletma Mar 27 at 12:58
  • 1
    Do not use your code in default.xml. Please use it in catalog_category_view.xml and catalogsearch_result_index.xml – Mohit Kumar Arora Mar 27 at 13:17
  • That's the trick. I'm used to make those changes in the local.xml from Magento1. Thought the default.xml in Magento2 working the same way. – bahaspletma Mar 27 at 13:29
  • Also click on right symbol (accept my answer). – Mohit Kumar Arora Mar 27 at 14:12
1

You can try this solutuions. first you need to update your list.phtml file path like :-

app/design/frontend/Vendor/Theme/Magento_Catalog/templates/product/list.phtml

Call Static block At the end of the file after toolbar. like this one :-

<?= $block->getToolbarHtml() ?>
<?php if (!$block->isRedirectToCartEnabled()) : ?>
    <script type="text/x-magento-init">
    {
        "[data-role=tocart-form], .form.map.checkout": {
            "catalogAddToCart": {
                "product_sku": "<?= /* @NoEscape */ $_product->getSku() ?>"
            }
        }
    }
    </script>
<?php endif; ?>
 <?php endif; ?>
  /* this is my cms block */ 
  <?php echo $block->getLayout()->createBlock('Magento\Cms\Bloc\Block')->setBlockId('identifire')->toHtml();?>

i call static block at end of the file.

  • Yes that works, but I try to achieve this via layout customization without modifing template files. It is easier to maintain from my point of view. – bahaspletma Mar 27 at 13:06
  • Ok Thank you.... – Magento_Bhurio Mar 27 at 18:02
0

Thanks Mohit for the hint. For anyone else who try to achive this, here is how I did it:

Copying /root/vendor/magento/module-catalog/view/frontend/layout/catalog_category_view.xml to my custom child theme folder /root/app/design/frontend/THEME_VENDOR/CHILD_THEME/Magento_Catalog/layout/catalog_category_view.xml. Opened the copied xml file and added my code within the referenceContainer name="content":

<referenceContainer name="content">

.
.
.

  <block class="Magento\Cms\Block\Block" name="MY-BLOCK-IDENTIFIER" after="-">
    <arguments>
        <argument name="block_id" xsi:type="string">MY-BLOCK-IDENTIFIER</argument>
    </arguments>
  </block>  
</referenceContainer>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.