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
-
can you please specify the magento version ?Shashank Kumrawat– Shashank Kumrawat2019年12月17日 10:42:57 +00:00Commented Dec 17, 2019 at 10:42
-
In Magento CE 2.3.3 @ShashankKumrawatHaerriz– Haerriz2019年12月17日 10:45:37 +00:00Commented 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?Munesh Jalwani– Munesh Jalwani2019年12月17日 11:24:48 +00:00Commented 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..Haerriz– Haerriz2019年12月17日 11:53:25 +00:00Commented Dec 17, 2019 at 11:53
-
@Haerriz Check my answer!Munesh Jalwani– Munesh Jalwani2019年12月17日 12:07:27 +00:00Commented Dec 17, 2019 at 12:07
1 Answer 1
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" />
-
It says that there's no function named as getSku(). @NaveedAsimHaerriz– Haerriz2019年12月17日 13:02:48 +00:00Commented Dec 17, 2019 at 13:02
-
you should check if sku is visible on listing and product view pageNaveed Asim– Naveed Asim2019年12月17日 13:15:25 +00:00Commented 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 @NaveedAsimHaerriz– Haerriz2019年12月17日 13:29:23 +00:00Commented Dec 17, 2019 at 13:29
-
Do I have extend view.php to achieve this answer? @NaveedAsimHaerriz– Haerriz2019年12月17日 14:03:50 +00:00Commented Dec 17, 2019 at 14:03
Explore related questions
See similar questions with these tags.