0

I have create a Magento Theme and also add attribute with a drop-down option value.

Now I wanted to display the value of the attribute in my Magento_theme::advancesearch.phtml file. i use in my advancesearch.phtml

/**
 * Get attribute info by attribute code and entity type
 */ 
$attributeCode = 'color';
$entityType = 'catalog_product';

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();

/*$attributeInfo = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class)
                               ->setCodeFilter($attributeCode)
                               ->getFirstItem();*/

$attributeInfo = $objectManager->get(\Magento\Eav\Model\Entity\Attribute::class)
                               ->loadByCode($entityType, $attributeCode);

//var_dump($attributeInfo->getData()); exit;

/**
 * Get all options name and value of the attribute
 */ 
$attributeId = $attributeInfo->getAttributeId();
$attributeOptionAll = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
                                    ->setPositionOrder('asc')
                                    ->setAttributeFilter($attributeId)                                               
                                    ->setStoreFilter()
                                    ->load();

//var_dump($attributeOptionAll->getData()); exit;

/**
 * Fetch particular option's name and value of the attribute
 */
$optionId = YOUR_OPTION_ID;                                  
$attributeOptionSingle = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
                                       ->setPositionOrder('asc')
                                       ->setAttributeFilter($attributeId)
                                       ->setIdFilter($optionId)
                                       ->setStoreFilter()
                                       ->load()
                                       ->getFirstItem();

//var_dump($attributeOptionSingle->getData()); exit;

But this showing nothing to my theme. Here is my default.xml,

<container name="header_advance_search" as="header_advance_search" label="header custom" htmlTag="div" htmlClass="advance_search">  
                <block class="Magento\Framework\View\Element\Template" name="advance_search" as="advance_search" template="Magento_Theme::advance_search.phtml"></block>
        </container>
        <move element="advance_search" destination="store.menu"/>

In Magento 1 I can use Mage: and get value of attribute product information and many more in any phtml file on Theme. But here face problem. Is there any solution?

3

Suppose i have a attribute like color and its options are red, blue, yellow.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$attr_code = 'color';
$attribute = $objectManager->create('\\Magento\\Catalog\\Model\\Product\\Attribute\\Repository')->get($attr_code);

/*returns all options red, blue, yellow*/
$options = $attribute->getOptions();

if($options){
    foreach($options as $option){
        echo $option->getValue(); // get id of option
        echo $option->getLabel(); // get Label of option
    }
}

if you want to get the option text (Option Label) using option id eg: 11 is the id of yellow.

//get option text (Option Label) using option id
$optionId = 11;
$optionText = $attribute->getSource()->getOptionText($optionId);
echo $optionText; // return string 'yellow'.

//get option id using option label.
$optionText = 'yellow';
$optionId = $attribute->getSource()->getOptionId($optionText);
echo $optionId; // 11.
| improve this answer | |
  • 1
    Its working, Please help me another thing, In magento we can use mage:: in anywhere any phtml file and get magento data for every sector, but is there possible to use that like mage: in magento 2? – Al-Mahbub Khan Jun 15 '17 at 22:10
  • No, in magento2 have no mage:: class, recommend to use dependency injection in block, helper then use it in needed phtml. – Sathish Jun 17 '17 at 5:15

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.