I have created one drop down attribute, I followed this tutorial to get selected values from that attribute. I successfully get the result, But I want to render whole drop down.What I do for achieve this?
Magento 2.0.1 – Render Dropdown from Product Dropdown Attribute
magento2productproduct-attribute
Related Solutions
I run into the same issue. After some debugging I found out that the values are used as an array index (e.g. debug the $object->toArray() in Magento\Framework\Model\ResourceModel\Db\AbstractDb::prepareDataForUpdate). At this place the information about the default value and sort order are already removed when using integers as value.
So easily do not use integers as values at this place. When using the backend to create/update an attribute manually, magento uses "option_1", "option_2" and so on for values.
Applied to your example, this would result in:
{
"option": {
"label": "Cappuccino",
"value": "option_6"
}
}
OT but related: Create an Attribute: When using POST /rest/V1/products/attributes to create an attribute having the options given, the options also won't be saved when using integers as values.
You could override the \Magento\Catalog\Block\Product\View\Attributes block and add a function to get the attribute set name:
Add something like this into overridden block:
public function __construct(
...
Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSet,
...) {
...
$this->attributeSet = $attributeSet
....
}
function getAttributeSet($_product) {
return $this->attributeSet->get($_product->getAttributeSetId())->getAttributeSetName();
}
Then from template you can grab a attribute set name and have more control over what attributes are shown for each product type:
$attributeSetName = $block->getAttributeSet($_product);
<?php if ($attributeSetName == "Fruit"): ?>
<tr>
<th class="col label" scope="row"><?php echo $_product->getResource()->getAttribute('attribute1')->getStoreLabel(); ?></th>
<td class="col data">
<?php echo $_product->getAttributeText('attribute1'); ?></a>
</td>
</tr>
<?php endif; ?>
<?php if ($attributeSetName == "Vegetable"): ?>
<tr>
<th class="col label" scope="row"><?php echo $_product->getResource()->getAttribute('attribute2')->getStoreLabel(); ?></th>
<td class="col data">
<?php echo $_product->getAttributeText('attribute2'); ?></a>
</td>
</tr>
<?php endif; ?>
File Location: Vendor/Theme/Magento_Catalog/templates/product/view/attributes.phtml
This will replace the grid within the product tabs.
Best Answer
On Product Detail Page or on Product List Page you can get attribute (color replace with yours) drop down value using following code: