1

I want add a Static block in product details page only for specific category how it will be achieved by XML layout ?

4
  • I want to show the block in product page for a specific category not to all categories. Hope it make sense. – Vinit Kumar Jan 9 '18 at 10:42
  • Did you see the name of layout example? it is catalog_category_view_id_12.xml where 12 number is category id you can replace with your category Id so this block will display only for this category. Make sense now? :) @Vinit – Keyur Shah Jan 9 '18 at 10:47
  • Thanks for reply ,Yes and it works for category list page but my question i want to show a static block contents to product details page with specific category @KeyurShah – Vinit Kumar Jan 9 '18 at 10:53
  • I am sorry It was my bad :) I misunderstood your question. As per I know I don't think it is possible by XML layout but you can achieve it from backend of the product but it seems length task for you @Vinit – Keyur Shah Jan 9 '18 at 10:56
3
  • Logged in to your backend and open that particular category where you would like to call that static block
  • Click on Design tab of the category -> Find the Layout Update Xml -> Put below code in that. It will called contact-us-info block on that particular category.

 <block class="Magento\Cms\Block\Block" name="test">
    <arguments>
        <argument name="block_id" xsi:type="string">contact-us-info</argument>
    </arguments>
</block>

  • Same way You can call your static block from there.
3
  • Thanks for the reply @ Manthan Dave Yes i did this and it is working the static block contents are showing on the product details page but i want to add this static block only for the specific category's products i have four categories with ids ( 10,11,12,13) i only want to display the static block for the products of category (id 12) not with others category's products . it it possible to manage by xml can i add condition xml – Vinit Kumar Jan 3 '18 at 6:29
  • Yes Thats what i am saying - Edit your category which have id 12 from backend and put this code in layout update xml file - So block will only display on that particular category. you don't need to write condition for that – Manthan Dave Jan 3 '18 at 6:31
  • I want to show the block in product page for a specific category not to all categories. Hope it make sense. – Vinit Kumar Jan 9 '18 at 10:42
2

Yes, you can achieve by this XML layout. for this you just need to create catalog_category_view_id_[ID].xml where [ID] is your category id. As per the comment, you need to display some static block in 12 number of category then you just need to create a layout XML file with catalog_category_view_id_12.xml in your theme > Magento_Catalog > layout directory and post below content to display CMS static block

<?xml version="1.0"?>
<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="content">
            <block class="Magento\Cms\Block\Block" name="test">
                <arguments>
                    <argument name="block_id" xsi:type="string">YOUR_CMS_BLOCK_IDENTIFIER</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

here I used <referenceContainer name="content"> you can use what you fell like it.

2
  • I want to show the block in product page for a specific category not to all categories. Hope it make sense. – Vinit Kumar Jan 9 '18 at 10:42
  • Did you see the name of layout example? it is catalog_category_view_id_12.xml where 12 number is category id you can replace with your category Id so this block will display only for this category. Make sense now? :) @VinitKumar – Keyur Shah Jan 9 '18 at 10:47
0

It's quite simple. You should create catalog_product_view_attribute_set_id_[ID].xml where [ID] is your category id. And simply add your Static block inside this layout. The block will be displayed on the product details page for specific category.

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.