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.

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'];
    }
?>

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.