3

I want to call a CMS Block via its identifier in a product attribute to display CMS block on product page.

  • Want to show different Static Block on different products

So how can I achieve this via attribute where I can enter identifier of CMS block?

Any help will be highly appreciated.

Update:

themeVendor/themeName/Magento_Catalog/templates/product/view/attributes.phtml

Block ID: shipping_test

attribute ID: size_map

<?php if ($_additional = $block->getAdditionalData()): ?>
<div class="additional-attributes-wrapper table-wrapper">
<table class="data table additional-attributes" id="product-attribute-specs-table">
<caption class="table-caption"><?php /* @escapeNotVerified */ echo __('More Information') ?></caption>
<tbody>
<?php foreach ($_additional as $_data): ?>
<?php if ($_data['label'] == "size_map") { ?>
<tr>
<th class="col label" scope="row"><?php echo $_data['label'] ?></th>
<td class="col data"><b class="mobilelabel"><?php echo $_data['label'] ?>: </b>
<?php /* @escapeNotVerified */ echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId($_data['value'])->toHtml() ?>
</td>
</tr>
<?php } else { ?>
<tr>
<th class="col label" scope="row"><?php echo $block->escapeHtml(__($_data['label'])) ?></th>
<td class="col data" data-th="<?php echo $block->escapeHtml(__($_data['label'])) ?>"><?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php } ?>
asked Sep 28, 2017 at 8:08
17
  • Is this within the attribute grid? Commented Sep 28, 2017 at 8:31
  • please check updated question Commented Sep 28, 2017 at 9:31
  • Im still a bit confused. In my example you will get a different static block on different products. It will depend on the attribute value. If the attribute value is shipping1 then the shipping1 static block is shown, shipping2 then the shipping2 block will show etc. Is this not what you want? Commented Sep 28, 2017 at 9:38
  • my mistake , its not shipping .... anything can be inside that block.... any information .... just need to show different blocks on different products .... Commented Sep 28, 2017 at 9:44
  • Well the blocks can be named anything and can include any information. its just an example... Do you intend to set the block based on an attribute within product? Commented Sep 28, 2017 at 9:47

1 Answer 1

4

You could create a template with below code:

<?php
$_product = $block->getProduct();
$shippingtype = $_product->getResource()->getAttribute('shippingtype')->getFrontend()->getValue($_product);
$shippingtypetext = $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId($shippingtype)->toHtml();
?>
<p><?php echo $shippingtypetext; ?></p>

You would need to create the 4 static blocks with static block identifiers with the same identifier as the attribute values.

Calling a static block based on attribute:

$block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId($shippingtype)->toHtml();

Will get a static block based on identifier. I grabbed an attribute value and used that within setBlockId($attrvalue) so that different static blocks show within different products depending on what block you reference in the attribute value.

Example within Frontend Attribute Grid Tab

For example within the attribute grid you could do something like below added within the foreach loop for attributes. Once the shipping_type is reached a new bit of code will be used that pulls a static block with that identifier rather than calling the attribute value:

 <?php if ($_data['code'] == "shipping_type"):?>
 <tr>
 <th class="col label" scope="row"><?php echo $_data['label'] ?></th>
 <td class="col data"><b class="mobilelabel"><?php echo $_data['label'] ?>: </b><?php /* @escapeNotVerified */ echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId($_data['value'])->toHtml()</td>
 </tr>
 <?php else: ?>
 <tr>
 <th class="col label" scope="row"><?php echo $_data['label'] ?></th>
 <td class="col data"><b class="mobilelabel"><?php echo $_data['label'] ?>: </b><?php /* @escapeNotVerified */ echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
 </tr>
 <?php endif; ?>

themeVendor/themeName/Magento_Catalog/templates/product/view/attributes.phtml

answered Sep 28, 2017 at 8:21
10
  • block content is not displaying. what can be the reason ? please Commented Sep 28, 2017 at 12:29
  • Maybe just echo the attribute code see that it matches. Try manually inputting the code that you know works to check the code for the block code is working. Commented Sep 28, 2017 at 12:31
  • Add your code to question so i can take a look also Commented Sep 28, 2017 at 12:32
  • see updated question, code added Commented Sep 28, 2017 at 12:37
  • 1
    No worries it's a handy feature tho im interested in improving as much as possible to and add to some stores. Commented Sep 29, 2017 at 11:48

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.