I am trying to display a static block based on the selection that has been made when adding a product in the magento backend.

So the situation is, I am adding the product and select a option from a dropdown attribute which has been created. The options are:

-Do not show table -Show table 1 -Show table 2 and so on...

Static blocks have been created realated to the dropdown options:

-Static block table 1 -Static block table 2 and so on...

I am using the following code in the phtml file which I want to display it:

<?php if
($_product->getAttributeText('tableselect') == "Table 1"): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('table_1_block')->toHtml() ?>
<?php endif; ?>

Its not working for me, what am i doing wrong here? Please help

Thanks in advance.


It may be that your product model does not contain the value. Try :

$choice = $_product->getResource()->getAttribute('tableselect')->getFrontend()->getValue($_product));

if ($choice == "Table 1") : ..

It is worth debugging to check your values e.g

<?php var_dump($_product->getData('tableselect')) ?>
<?php var_dump($choice) ?>
  • Thanks for your input, I have managed to get this to work and as suggested I had to include the value in the model. – AlphaOne Jun 18 '16 at 20:04
  • Should this code also work on Magento 2? – Luis Garcia Jun 24 '16 at 20:54

I managed to get this to work. As stated by Fisheye Chris I had to make sure it was in the product model and used in the frontend. This is my working code now:

if ($this->getProduct()->getAttributeText('tableselect') == "Table 1") {
    echo $this->getLayout()->createBlock('cms/block')->setBlockId('table_1_block')->toHtml();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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