How can I add Configurable Product to shopping cart in product view page ? I try this code to show the custom option.`

$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>";

Custom options are shown but when I tried to add to cart, it doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.