I have a created different CMS static blocks. I need to call the respective static block for each product.
I thought of getting the URL key and then assign it to the respective Products. How could I achieve this ?
Thanks in advance for the answers
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI have a created different CMS static blocks. I need to call the respective static block for each product.
I thought of getting the URL key and then assign it to the respective Products. How could I achieve this ?
Thanks in advance for the answers
Best possible solution would be to create static blocks with product skus. Create a template and call these static block in that template and add that template in layout, where you want to place the block.
template will be something like :
<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
?>
<div class="product-block">
<?php
echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId($_product->getSku())->toHtml();
?>
</div>
and place this in your catalog_product_view.xml layout where you want to show the block :
<block class="Magento\Catalog\Block\Product\View" name="product.static.block" as="product_static_block" template="Magento_Catalog::product/view/product_static_block.phtml" />