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.



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>
<?php foreach ($_additional as $_data): ?>

<?php if ($_data['label'] == "size_map") { ?>

<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() ?>

<?php } else { ?>

<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>

<?php } ?>
  • Is this within the attribute grid?
    – harri
    Sep 28, 2017 at 8:31
  • please check updated question 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?
    – harri
    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 .... 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?
    – harri
    Sep 28, 2017 at 9:47

1 Answer 1


You could create a template with below code:

$_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:


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"):?>
                <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>
        <?php else: ?>
                <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>
     <?php endif; ?>


  • block content is not displaying. what can be the reason ? please 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.
    – harri
    Sep 28, 2017 at 12:31
  • Add your code to question so i can take a look also
    – harri
    Sep 28, 2017 at 12:32
  • see updated question, code added 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.
    – harri
    Sep 29, 2017 at 11:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.