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.

Mohit Kumar Arora
10.2k7 gold badges29 silver badges57 bronze badges
asked Jul 22, 2016 at 9:55

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;?>
answered Jul 22, 2016 at 12:17
3
  • I used your multiselect attribute code & i get error like "Call to undefined function getAttributeText()" 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. Commented Jul 22, 2016 at 12:46
  • @Ramya U r right and updated ans pls check now 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')
answered Jul 22, 2016 at 12:21
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)
answered Jul 22, 2016 at 12:04
2
  • Can you explain briefly? Commented Jul 22, 2016 at 12:08
  • Please check my modified answer. Commented Jul 22, 2016 at 12:14

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.