'Magento 2 : Allow customer to edit custom options on Cart

I am trying to add functionality to allow a customer to edit options.

I have created a Module Vendor/COptions. This is loading custom options and select, however is not getting user selected options.

I would like to know how to received selected options and check if the option is selected. At the moment this line in Select.php is not receiving anything, and the variable $checked is null.

$configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId());

in Vendor/COptions\view\frontend\templates\cart\item\default.phtml :

 <?php $selectedOptions = $block->getSelectedOptionList(); ?>
 <?php //$_options = Mage::helper('core')->decorateArray($block>getOptions())
    $_options =  $block->getOptions() ?>  
  <?php if ($_options AND count($_options)):?>
    <dl>
   <?php foreach($_options as $_option): ?>
  <?php echo $this->getOptionHtml($_option) ?>
   <?php endforeach; ?>
   </dl>
  <?php endif; ?>

in Vendor\COptions\Helper\Rewrite\Product:

public function getCustomOptions(\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item, $simple="")
{

    $product = $item->getProduct();
    $coptions = [];
    $coptions = $product->getOptions();
    $options = array();

   if ($coptions) { 
   foreach ($coptions as $option) {
   $itemOption = $item->getOptionByCode('option_' . $option->getId());

           /** @var $group \Magento\Catalog\Model\Product\Option\Type\DefaultType */
                  $group = $option->groupFactory($option->getType())
                      ->setOption($option)
                      ->setConfigurationItem($item)
                      ->setConfigurationItemOption($itemOption);



                  if ('file' == $option->getType()) {
                      $downloadParams = $item->getFileDownloadParams();
                      if ($downloadParams) {
                          $url = $downloadParams->getUrl();
                          if ($url) {
                              $group->setCustomOptionDownloadUrl($url);
                          }
                          $urlParams = $downloadParams->getUrlParams();
                          if ($urlParams) {
                              $group->setCustomOptionUrlParams($urlParams);
                          }
                      }
                  }


  if($simple == "Y"){


    array_push($options, $itemOption['value']);
  }else{
                  $options[] = [
                      'label' => $option->getTitle(),
                      'value' => $group->getFormattedOptionValue($itemOption['value']),
                      'print_value' => $group->getFormattedOptionValue($itemOption['value']),
                      'option_id' => $option->getId(),
                      'option_type' => $option->getType(),
                      'custom_view' => $group->isCustomizedView(),
                  ];
}               
}
}
 $addOptions = $item->getOptionByCode('additional_options');
      if ($addOptions) {
          $options = array_merge($options, $this->serializer->unserialize($addOptions->getValue()));
      }
      return $options;
  }

in Vendor/COptions\Block\Rewrite\Catalog\Product\View\Options\Type\Select.php:

    public function getValuesHtml()
  {
    $_option = $this->getOption();
    $configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId());
    $store = $this->getProduct()->getStore();

    $this->setSkipJsReloadPrice(1);
    // Remove inline prototype onclick and onchange events

    if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN ||
        $_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE
    ) {
        $require = $_option->getIsRequire() ? ' required' : '';
        $extraParams = '';
        $select = $this->getLayout()->createBlock(
            \Magento\Framework\View\Element\Html\Select::class
        )->setData(
            [
                'id' => 'select_' . $_option->getId(),
                'class' => $require . ' product-custom-option admin__control-select'
            ]
        );
        if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN) {
            $select->setName('options[' . $_option->getid() . ']')->addOption('', __('-- Please Select --'));
        } else {
            $select->setName('options[' . $_option->getid() . '][]');
            $select->setClass('multiselect admin__control-multiselect' . $require . ' product-custom-option');
        }
        foreach ($_option->getValues() as $_value) {
            $priceStr = $this->_formatPrice(
                [
                    'is_percent' => $_value->getPriceType() == 'percent',
                    'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'),
                ],
                false
            );
            $select->addOption(
                $_value->getOptionTypeId(),
                $_value->getTitle() . ' ' . strip_tags($priceStr) . '',
                ['price' => $this->pricingHelper->currencyByStore($_value->getPrice(true), $store, false)]
            );
        }
        if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE) {
            $extraParams = ' multiple="multiple"';
        }
        if (!$this->getSkipJsReloadPrice()) {
            $extraParams .= ' onchange="opConfig.reloadPrice()"';
        }
        $extraParams .= ' data-selector="' . $select->getName() . '"';
        $select->setExtraParams($extraParams);

        if ($configValue) {
            $select->setValue($configValue);
        }

        return $select->getHtml();
    }

    if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_RADIO ||
        $_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_CHECKBOX
    ) {


        $selectHtml = '<div class="options-list nested" id="options-' . $_option->getId() . '-list">';
        $require = $_option->getIsRequire() ? ' required' : '';
        $arraySign = '';
        switch ($_option->getType()) {
            case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_RADIO:
                $type = 'radio';
                $class = 'radio admin__control-radio';
                if (!$_option->getIsRequire()) {
                    $selectHtml .= '<div class="field choice admin__field admin__field-option">' .
                        '<input type="radio" id="options_' .
                        $_option->getId() .
                        '" class="' .
                        $class .
                        ' product-custom-option" name="options[' .
                        $_option->getId() .
                        ']"' .
                        ' data-selector="options[' . $_option->getId() . ']"' .
                        ($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"') .
                        ' value="" checked="checked" /><label class="label admin__field-label" for="options_' .
                        $_option->getId() .
                        '"><span>' .
                        __('None') . '</span></label></div>';
                }
                break;
            case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_CHECKBOX:
                $type = 'checkbox';
                $class = 'checkbox admin__control-checkbox';
                $arraySign = '[]';
                break;
        }
        $count = 1;
        foreach ($_option->getValues() as $_value) {
            $count++;

            $priceStr = $this->_formatPrice(
                [
                    'is_percent' => $_value->getPriceType() == 'percent',
                    'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'),
                ]
            );

            $htmlValue = $_value->getOptionTypeId();
            if ($arraySign) {
                $checked = is_array($configValue) && in_array($htmlValue, $configValue) ? 'checked' : '';
            } else {
                $checked = $configValue == $htmlValue ? 'checked' : '';
            }


            $dataSelector = 'options[' . $_option->getId() . ']';
            if ($arraySign) {
                $dataSelector .= '[' . $htmlValue . ']';
            }

            $selectHtml .= '<div class="field choice admin__field admin__field-option' .
                $require .
                '">' .
                '<input type="' .
                $type .
                '" class="' .
                $class .
                ' ' .
                $require .
                ' product-custom-option"' .
                ($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"') .
                ' name="options[' .
                $_option->getId() .
                ']' .
                $arraySign .
                '" id="options_' .
                $_option->getId() .
                '_' .
                $count .
                '" value="' .
                $htmlValue .
                '" ' .
                $checked .
                ' data-selector="' . $dataSelector . '"' .
                ' price="' .
                $this->pricingHelper->currencyByStore($_value->getPrice(true), $store, false) .
                '" />' .
                '<label class="label admin__field-label" for="options_' .
                $_option->getId() .
                '_' .
                $count .
                '"><span>' .
                $_value->getTitle() .
                '</span> ' .
                $priceStr .
                '</label>';
            $selectHtml .= '</div>';
        }
        $selectHtml .= '</div>';

        return $selectHtml;
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source