Magento 2 How can I show custom options on product list page?
-
15-04-2021 - |
Question
Like given in this blog for magento1 i want to add custom options in list page for magento 2. is there any module available for same or can someone guide me to achieve this?
Solution
I've implement code for it. Please create block class Don't use Object Manager directly on template. I've used this for demo.
<?php
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customOptions = $_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($_product);
$optStr = "";
foreach($customOptions as $optionKey => $optionVal):
$optStr .= "<div class='custom-options'><label>".$optionVal->getTitle()." </label>";
$optStr .= "<select name='options[".$optionVal->getId()."]'>";
foreach($optionVal->getValues() as $valuesKey => $valuesVal) {
$optStr .= "<option value='".$valuesVal->getId()."'>".$valuesVal->getTitle()."</option>";
}
$optStr .= "</select></div>";
endforeach;
echo($optStr );
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange