Magento 2:允许客户在购物车上编辑自定义选项

时间:2018-03-11 10:37:37

标签: php magento module magento2

我正在尝试添加允许客户编辑选项的功能。

我创建了一个模块供应商/组件。 这是加载自定义选项并选择,但是没有获得用户选择的选项。

我想知道如何接收所选选项并检查是否选择了该选项。 目前,Select.php中的这一行没有收到任何内容,变量$ checked为null。

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

在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; ?>

在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;
  }

在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;
    }
}

0 个答案:

没有答案