1

I want one dropdown value in Product view page in Magento 2. For e.g, if I have select custom dropdown value in the product from the admin section. Now I want this value in view page as an option to display.

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

$sizes = $productRepository->get('supply')->getOptions();
foreach ($sizes as $sizesOption) {
    if ($sizesOption->getValue() == '8') {
        echo    $sizesOption->getValue();
  echo  $sizesOption->getLabel();
}

}

But currently, they displayed all value in all product, not particular product value as per assing into products. I want this value filter by current product.

  • get attribute option ?? – Rk Rathod May 8 at 15:35
1

Try following way to getting a selected dropdown text for product:

<?= $_product->getAttributeText("country_of_manufacture") ?>
  • It's Working perfectly. – Nikul May 9 at 7:11
0

You can get product attribute option Id and option label using product object

Get option label based on option id from product object

$optionId = 10;

$attr = $_product->getResource()->getAttribute('color');
if ($attr->usesSource()) {
   $optionText = $attr->getSource()->getOptionText($optionId);
}

It will return option text ex. Red

Get option id based on option label

$attr = $_product->getResource()->getAttribute('color');
if ($attr->usesSource()) {
   $option_id = $attr->getSource()->getOptionId("Red");
}

It will return option id ex. 10

Use below code to check is attribute exist

$isAttributeExist = $_product->getResource()->getAttribute($attributeCode);

For more reference check this blog Get attrubute (Rakesh jesadiya)

I hope it works!

0

In Magento 2 On Product Detail Page or on Product List Page you can get attribute with drop down value using following code:

  <?php 

    $attr = $_product->getResource()->getAttribute('delivery_times')->getOptions(); 
    echo '<select name="delivery-time">';
    echo '<option selected="selected">Delivery Time</option>';
    foreach ($attr as $attrDeliveryTimes) {
      if($attrDeliveryTimes->getLabel() > 0):
      echo'<option value="'.$attrDeliveryTimes->getLabel().'">'.$attrDeliveryTimes->getLabel().'</option>';
      //echo $attrDeliveryTimes->getValue();
      //echo $attrDeliveryTimes->getLabel();
      endif;
    }
    echo '</select>';   

?>

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.