Magento 2 How can I show custom options on product list page?
-
15-04-2021 - |
Domanda
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?
Soluzione
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 );
?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange