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

Partab Saifuddin Zakir
1,3021 gold badge21 silver badges47 bronze badges
asked Dec 17, 2019 at 10:38
7
  • can you please specify the magento version ? Commented Dec 17, 2019 at 10:42
  • In Magento CE 2.3.3 @ShashankKumrawat Commented Dec 17, 2019 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? Commented Dec 17, 2019 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.. Commented Dec 17, 2019 at 11:53
  • @Haerriz Check my answer! Commented Dec 17, 2019 at 12:07

1 Answer 1

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" />
answered Dec 17, 2019 at 12:29
4
  • It says that there's no function named as getSku(). @NaveedAsim Commented Dec 17, 2019 at 13:02
  • you should check if sku is visible on listing and product view page Commented Dec 17, 2019 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 Commented Dec 17, 2019 at 13:29
  • Do I have extend view.php to achieve this answer? @NaveedAsim Commented Dec 17, 2019 at 14:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.