1

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

7
  • can you please specify the magento version ? – Shashank Kumrawat Dec 17 '19 at 10:42
  • In Magento CE 2.3.3 @ShashankKumrawat – Haerriz Dec 17 '19 at 10:45
  • @Haerriz you want cms block on product detail page, like while on checkout its shows near add to cart button? is this you want? – Munesh Jalwani Dec 17 '19 at 11:24
  • yes, you are right @MuneshJalwani but every CMS static blocks is related to its relevent products , so its necessary to call the respective static block to its relevent product detail page.. – Haerriz Dec 17 '19 at 11:53
  • @Haerriz Check my answer! – Munesh Jalwani Dec 17 '19 at 12:07
0

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" />
4
  • It says that there's no function named as getSku(). @NaveedAsim – Haerriz Dec 17 '19 at 13:02
  • you should check if sku is visible on listing and product view page – Naveed Asim Dec 17 '19 at 13:15
  • Sku is visible in product view and in product listing in front end but I phpstorm says that there no function named as getsku(). inside $product @NaveedAsim – Haerriz Dec 17 '19 at 13:29
  • Do I have extend view.php to achieve this answer? @NaveedAsim – Haerriz Dec 17 '19 at 14:03

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.