|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DK\CustomOptionDefaultValue\Block\Product\View\Options\Type\Select; |
| 6 | + |
| 7 | +use Magento\Catalog\Block\Product\View\Options\AbstractOptions; |
| 8 | +use Magento\Catalog\Model\Product\Option; |
| 9 | +use Magento\Framework\View\Element\Html\Select; |
| 10 | + |
| 11 | +class Multiple extends AbstractOptions |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var bool |
| 15 | + */ |
| 16 | + private $existConfigValue = false; |
| 17 | + |
| 18 | + /** |
| 19 | + * {@inheritdoc} |
| 20 | + * |
| 21 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 22 | + * |
| 23 | + * @return string |
| 24 | + */ |
| 25 | + protected function _toHtml() |
| 26 | + { |
| 27 | + $option = $this->getOption(); |
| 28 | + $optionType = $option->getType(); |
| 29 | + $configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId()); |
| 30 | + $require = $option->getIsRequire() ? ' required' : ''; |
| 31 | + $extraParams = ''; |
| 32 | + |
| 33 | + if ($configValue) { |
| 34 | + $this->existConfigValue = true; |
| 35 | + } |
| 36 | + |
| 37 | + /** @var Select $select */ |
| 38 | + $select = $this->getLayout()->createBlock( |
| 39 | + Select::class |
| 40 | + )->setData( |
| 41 | + [ |
| 42 | + 'id' => 'select_' . $option->getId(), |
| 43 | + 'class' => $require . ' product-custom-option admin__control-select', |
| 44 | + ] |
| 45 | + ); |
| 46 | + $select = $this->insertSelectOption($select, $option); |
| 47 | + $select = $this->processSelectOption($select, $option); |
| 48 | + |
| 49 | + if ($optionType === Option::OPTION_TYPE_MULTIPLE) { |
| 50 | + $extraParams = ' multiple="multiple"'; |
| 51 | + } |
| 52 | + if (!$this->getSkipJsReloadPrice()) { |
| 53 | + $extraParams .= ' onchange="opConfig.reloadPrice()"'; |
| 54 | + } |
| 55 | + |
| 56 | + $extraParams .= ' data-selector="' . $select->getName() . '"'; |
| 57 | + $select->setExtraParams($extraParams); |
| 58 | + |
| 59 | + if ($this->existConfigValue) { |
| 60 | + $select->setValue($configValue); |
| 61 | + } |
| 62 | + |
| 63 | + return $select->getHtml(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns select with inserted option give as a parameter |
| 68 | + */ |
| 69 | + private function insertSelectOption(Select $select, Option $option): Select |
| 70 | + { |
| 71 | + $require = $option->getIsRequire() ? ' required' : ''; |
| 72 | + if ($option->getType() === Option::OPTION_TYPE_DROP_DOWN) { |
| 73 | + $select->setName('options[' . $option->getId() . ']')->addOption('', __('-- Please Select --')); |
| 74 | + } else { |
| 75 | + $select->setName('options[' . $option->getId() . '][]'); |
| 76 | + $select->setClass('multiselect admin__control-multiselect' . $require . ' product-custom-option'); |
| 77 | + } |
| 78 | + |
| 79 | + return $select; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns select with formatted option prices |
| 84 | + */ |
| 85 | + private function processSelectOption(Select $select, Option $option): Select |
| 86 | + { |
| 87 | + $store = $this->getProduct()->getStore(); |
| 88 | + foreach ($option->getValues() as $_value) { |
| 89 | + $isPercentPriceType = $_value->getPriceType() === 'percent'; |
| 90 | + $priceStr = $this->_formatPrice( |
| 91 | + [ |
| 92 | + 'is_percent' => $isPercentPriceType, |
| 93 | + 'pricing_value' => $_value->getPrice($isPercentPriceType), |
| 94 | + ], |
| 95 | + false |
| 96 | + ); |
| 97 | + |
| 98 | + $selectOption = []; |
| 99 | + if (!$this->existConfigValue && $_value->getData('is_default')) { |
| 100 | + $selectOption = ['selected' => 'selected']; |
| 101 | + } |
| 102 | + |
| 103 | + $select->addOption( |
| 104 | + $_value->getOptionTypeId(), |
| 105 | + $_value->getTitle() . ' ' . strip_tags($priceStr) . '', |
| 106 | + [ |
| 107 | + 'price' => $this->pricingHelper->currencyByStore( |
| 108 | + $_value->getPrice(true), |
| 109 | + $store, |
| 110 | + false |
| 111 | + ), |
| 112 | + $selectOption, |
| 113 | + ] |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + return $select; |
| 118 | + } |
| 119 | +} |
0 commit comments