Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top