1

I achieved this funcionality in Magento 1 doing the following:

  1. Created an attribute "sizetable" with options "tshirts" and "dresses".
  2. Assigned the respective attribute value for each product.
  3. Created 2 blocks with identifiers "tshirts" and "dresses".
  4. On the product template I added the following code:

<?php $sizeguide = $_product->getAttributeText('sizetable'); if ($sizeguide): ?> <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($sizeguide)->toHtml() ?> <?php endif; ?>

However this process does not work in Magento 2.

2 Answers 2

3
+50

your block calling is incorrect, In Magento 2 it should be called as

$this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId($sizeguide)->toHtml()

I followed all your steps and added following code to template file, and worked fine.

/* $_product = $block->getProduct(); */
$sizeguide = $_product->getAttributeText('sizetable');
if ($sizeguide):
    echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId($sizeguide)->toHtml();
endif;
1
  • It works if the first line is uncommented Commented Jul 28, 2016 at 16:49
0

You can also do like this.

$_product = $block->getProduct(); 
$attribute = $_product->getResource()->getAttribute('sizetable');
if ($attribute)
{
    $sizetable = $attribute ->getFrontend()->getValue($_product);
    echo $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId($sizeguide)->toHtml();
}

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.