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:
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
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;?>
-
I used your multiselect attribute code & i get error like "Call to undefined function getAttributeText()"Ramya– Ramya2016年07月22日 12:42:04 +00:00Commented 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– Ramya2016年07月22日 12:46:52 +00:00Commented Jul 22, 2016 at 12:46
-
@Ramya U r right and updated ans pls check nowAbdul– Abdul2016年07月22日 13:02:35 +00:00Commented Jul 22, 2016 at 13:02
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')
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)
-
-
Please check my modified answer.Mohit Kumar Arora– Mohit Kumar Arora2016年07月22日 12:14:39 +00:00Commented Jul 22, 2016 at 12:14
Explore related questions
See similar questions with these tags.