3

I'm new to Magento and I'm developing E-commerce site in Magento 2.

I have created an attribute "Power Type" (MultiSelect) and gave 4 options to it and assigned it to "default" Attribute set.

I created a product with "default" attribute selected and selected 3 options from the 4 options given in "Power Type" attribute. Now when I show these options on Product view,

I want to show all of them with the option that is not selected to be grayed out.

Right now I'm using following code in my phtml file to retrieve the attribute values

$_powertypes = $_product->getAttributeText("power_types");

Above code gives me value of that attribute that is for a product selected. I want all the four options that were made during the creation of attribute.

Thanks in advance.

Rakesh Jesadiya
42.5k19 gold badges135 silver badges186 bronze badges
asked Sep 20, 2016 at 10:58

1 Answer 1

2

You can just keep below code reference to get all option of attribute,

Inside your block file,

<?php
 protected $eavAttributeRepository;
 public function __construct(
 \Magento\Catalog\Block\Product\Context $context, 
 \Magento\Eav\Api\AttributeRepositoryInterface $eavAttributeRepository
 ) {
 parent::__construct($context);
 $this->eavAttributeRepository = $eavAttributeRepository;
 }
 public function getOptionlist($attributeCode){
 $attributes = $this->eavAttributeRepository->get(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,$attributeCode);
 $options = $attributes->getSource()->getAllOptions(false);
 return $options; 
 }
}
?>

Inside your phtml file,

<?php
 $optionList = $block->getOptionlist('power_types');
 foreach ($optionList as $value) {
 //print_r($value);
 echo $value['label'];
 }
?>
answered Sep 20, 2016 at 12:58
0

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.