0

I want to create an multiselect attribute with 3 values. When I select the values during product creation It should display the content from the static blocks. So I entered the identifier of the static block under default store view (which is English below) option during attribute creation like:

enter image description here

Now in view.phtml, I have added the below code:

<?php
getResource()->getAttribute('r_n')->getFrontend()->getValue($_product);
echo $this->getLayout()->createBlock('cms/block')->setBlockID($cmsstatic)->tohtml();
?>

where r_n is the attribute code.

But am getting error like:

Fatal error: Call to undefined function getResource()

Kindly someone help me to resolve this issue.

3 Answers 3

1

Try this

For select attribute

<?php $attributeValue = $_product->getAttributeText('r_n');?>
<?php if($attributeValue):?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockID($attributeValue)->tohtml(); ?>
<?php endif;?>

For multiselect attribute

<?php $attributeValues = $_product->getAttributeText('r_n');?>
<?php if(is_array($attributeValues) & !empty($attributeValues)):?>
<?php foreach($attributeValues as $attributeValue):?>
    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockID($attributeValue)->tohtml(); ?>
<?php endforeach;?>
<?php elseif($attributeValues):?>
    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockID($attributeValues)->tohtml(); ?>
<?php endif;?>
3
  • I used your multiselect attribute code & i get error like "Call to undefined function getAttributeText()"
    – Ramya
    Commented Jul 22, 2016 at 12:42
  • hey your code has very small syntax error: $_product>getAttributeText('r_n') The arrow is not proper. It should be like : $_product->getAttributeText('r_n') Now its perfectlyy working. Thank you.
    – Ramya
    Commented Jul 22, 2016 at 12:46
  • @Ramya U r right and updated ans pls check now
    – Abdul
    Commented Jul 22, 2016 at 13:02
2

In order to get the option label of an attribute value of the current product in catalog/product/view.phtml you can also use:

$_product->getAttributeText('r_n')
1

Before getResource() you need to use object of class in which this function has been created.

For example if you are trying to get value of a product attribute, then it should be:

$_product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product)
2
  • Can you explain briefly?
    – Ramya
    Commented Jul 22, 2016 at 12:08
  • Please check my modified answer. Commented Jul 22, 2016 at 12:14

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.