1

I have added an attribute (Dropdown) at the admin side, and now I want to get that dropdown on my product detail page in the module, with there option and values. I have searched everywhere but I couldn't find it, help me here. I am adding the screenshot of the dropdown from the admin side. enter image description here

and screenshot where I want to show it, you see the dropdown. (This is custom create by me ) enter image description here

2
  • do you need this under review section in the product detail page right?
    – Hiren
    Commented Jan 10, 2020 at 8:55
  • right now, i just need the dropdown (Option and values)
    – Ali Raza
    Commented Jan 10, 2020 at 9:55

2 Answers 2

3

Use bellow code to achieve your goal,

do not use objectManager in actual implementation.

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$object = $objectManager->create('\Magento\Eav\Model\Config');

$attribute = $object->getAttribute('catalog_product', 'test'); // replace 'test' with your product attribute name
$options = $attribute->getSource()->getAllOptions();
?>

<select id="select">
    <?php 
    foreach ($options as $option):
        $value = $option["value"];
        $label = $option["label"];
        echo "<option value='$value'>$label</option>";   
    endforeach;
    ?>
</select>
2
  • Thank you very much, this is working, But I dont want to use Object Manager.
    – Ali Raza
    Commented Jan 10, 2020 at 9:54
  • 1
    create a custom function in block class and use this function in template file
    – Hiren
    Commented Jan 10, 2020 at 9:56
0

I have solved it With the help of @Hiren. he gives me a lead to use of eva/Model.

here what i did.

Created file Vendor\Module\Block\Options.php

<?php
namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;

class Options extends \Magento\Contact\Block\ContactForm
{

    protected $eavAttributeFactory;
    protected $attributeOptionManagement;

    public function __construct(
        \Magento\Eav\Model\Entity\AttributeFactory $eavAttributeFactory,
        \Magento\Eav\Api\AttributeOptionManagementInterface $attributeOptionManagement,
        \Magento\Framework\View\Element\Template\Context $context
    ) {
        parent::__construct($context);
        $this->eavAttributeFactory = $eavAttributeFactory;
        $this->attributeOptionManagement = $attributeOptionManagement;
    }
    public function getDropdown(){
        $magentoAttributeOptions = $this->attributeOptionManagement->getItems(
            'catalog_product','platform');
        return $magentoAttributeOptions;
    }
}

phtml file

<?php
$dropdown = $block->getDropdown();
?>
<select id="select_platform">
    <option value="0">Please Select Platform</option>
    <?php
    foreach ($dropdown as $option):
        $value = $option["value"];
        $label = $option["label"];
        echo "<option value='$value'>$label</option>";
    endforeach;
    ?>

1
  • accept the answer
    – Hiren
    Commented Jan 10, 2020 at 11:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.