|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | + |
6 | 7 | namespace Magento\Catalog\Block\Product\View\Options\Type;
|
7 | 8 |
|
| 9 | +use Magento\Catalog\Api\Data\ProductCustomOptionInterface; |
| 10 | +use Magento\Catalog\Block\Product\View\Options\Type\Select\CheckableFactory; |
| 11 | +use Magento\Catalog\Block\Product\View\Options\Type\Select\MultipleFactory; |
| 12 | +use Magento\Framework\App\ObjectManager; |
| 13 | +use Magento\Framework\View\Element\Template\Context; |
| 14 | +use Magento\Framework\Pricing\Helper\Data; |
| 15 | +use Magento\Catalog\Helper\Data as CatalogHelper; |
| 16 | + |
8 | 17 | /**
|
9 | 18 | * Product options text type block
|
10 | 19 | *
|
|
13 | 22 | */
|
14 | 23 | class Select extends \Magento\Catalog\Block\Product\View\Options\AbstractOptions
|
15 | 24 | {
|
| 25 | + /** |
| 26 | + * @var CheckableFactory |
| 27 | + */ |
| 28 | + private $checkableFactory; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var MultipleFactory |
| 32 | + */ |
| 33 | + private $multipleFactory; |
| 34 | + |
| 35 | + /** |
| 36 | + * Select constructor. |
| 37 | + * @param Context $context |
| 38 | + * @param Data $pricingHelper |
| 39 | + * @param CatalogHelper $catalogData |
| 40 | + * @param array $data |
| 41 | + * @param CheckableFactory|null $checkableFactory |
| 42 | + * @param MultipleFactory|null $multipleFactory |
| 43 | + */ |
| 44 | + public function __construct( |
| 45 | + Context $context, |
| 46 | + Data $pricingHelper, |
| 47 | + CatalogHelper $catalogData, |
| 48 | + array $data = [], |
| 49 | + CheckableFactory $checkableFactory = null, |
| 50 | + MultipleFactory $multipleFactory = null |
| 51 | + ) { |
| 52 | + parent::__construct($context, $pricingHelper, $catalogData, $data); |
| 53 | + $this->checkableFactory = $checkableFactory ?: ObjectManager::getInstance()->get(CheckableFactory::class); |
| 54 | + $this->multipleFactory = $multipleFactory ?: ObjectManager::getInstance()->get(MultipleFactory::class); |
| 55 | + } |
| 56 | + |
16 | 57 | /**
|
17 | 58 | * Return html for control element
|
18 | 59 | *
|
19 | 60 | * @return string
|
20 |
| - * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
21 |
| - * @SuppressWarnings(PHPMD.NPathComplexity) |
22 |
| - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
23 | 61 | */
|
24 |
| - public function getValuesHtml() |
| 62 | + public function getValuesHtml(): string |
25 | 63 | {
|
26 |
| - $_option = $this->getOption(); |
27 |
| - $configValue = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $_option->getId()); |
28 |
| - $store = $this->getProduct()->getStore(); |
29 |
| - |
30 |
| - $this->setSkipJsReloadPrice(1); |
31 |
| - // Remove inline prototype onclick and onchange events |
| 64 | + $option = $this->getOption(); |
| 65 | + $optionType = $option->getType(); |
32 | 66 |
|
33 |
| - if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN || |
34 |
| - $_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE |
| 67 | + if ($optionType === ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN || |
| 68 | + $optionType === ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE |
35 | 69 | ) {
|
36 |
| - $require = $_option->getIsRequire() ? ' required' : ''; |
37 |
| - $extraParams = ''; |
38 |
| - $select = $this->getLayout()->createBlock( |
39 |
| - \Magento\Framework\View\Element\Html\Select::class |
40 |
| - )->setData( |
41 |
| - [ |
42 |
| - 'id' => 'select_' . $_option->getId(), |
43 |
| - 'class' => $require . ' product-custom-option admin__control-select' |
44 |
| - ] |
45 |
| - ); |
46 |
| - if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN) { |
47 |
| - $select->setName('options[' . $_option->getId() . ']')->addOption('', __('-- Please Select --')); |
48 |
| - } else { |
49 |
| - $select->setName('options[' . $_option->getId() . '][]'); |
50 |
| - $select->setClass('multiselect admin__control-multiselect' . $require . ' product-custom-option'); |
51 |
| - } |
52 |
| - foreach ($_option->getValues() as $_value) { |
53 |
| - $priceStr = $this->_formatPrice( |
54 |
| - [ |
55 |
| - 'is_percent' => $_value->getPriceType() == 'percent', |
56 |
| - 'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'), |
57 |
| - ], |
58 |
| - false |
59 |
| - ); |
60 |
| - $select->addOption( |
61 |
| - $_value->getOptionTypeId(), |
62 |
| - $_value->getTitle() . ' ' . strip_tags($priceStr) . '', |
63 |
| - ['price' => $this->pricingHelper->currencyByStore($_value->getPrice(true), $store, false)] |
64 |
| - ); |
65 |
| - } |
66 |
| - if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE) { |
67 |
| - $extraParams = ' multiple="multiple"'; |
68 |
| - } |
69 |
| - if (!$this->getSkipJsReloadPrice()) { |
70 |
| - $extraParams .= ' onchange="opConfig.reloadPrice()"'; |
71 |
| - } |
72 |
| - $extraParams .= ' data-selector="' . $select->getName() . '"'; |
73 |
| - $select->setExtraParams($extraParams); |
74 |
| - |
75 |
| - if ($configValue) { |
76 |
| - $select->setValue($configValue); |
77 |
| - } |
78 |
| - |
79 |
| - return $select->getHtml(); |
| 70 | + $optionBlock = $this->multipleFactory->create(); |
80 | 71 | }
|
81 | 72 |
|
82 |
| - if ($_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_RADIO || |
83 |
| - $_option->getType() == \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_CHECKBOX |
| 73 | + if ($optionType === ProductCustomOptionInterface::OPTION_TYPE_RADIO || |
| 74 | + $optionType === ProductCustomOptionInterface::OPTION_TYPE_CHECKBOX |
84 | 75 | ) {
|
85 |
| - $selectHtml = '<div class="options-list nested" id="options-' . $_option->getId() . '-list">'; |
86 |
| - $require = $_option->getIsRequire() ? ' required' : ''; |
87 |
| - $arraySign = ''; |
88 |
| - switch ($_option->getType()) { |
89 |
| - case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_RADIO: |
90 |
| - $type = 'radio'; |
91 |
| - $class = 'radio admin__control-radio'; |
92 |
| - if (!$_option->getIsRequire()) { |
93 |
| - $selectHtml .= '<div class="field choice admin__field admin__field-option">' . |
94 |
| - '<input type="radio" id="options_' . |
95 |
| - $_option->getId() . |
96 |
| - '" class="' . |
97 |
| - $class . |
98 |
| - ' product-custom-option" name="options[' . |
99 |
| - $_option->getId() . |
100 |
| - ']"' . |
101 |
| - ' data-selector="options[' . $_option->getId() . ']"' . |
102 |
| - ($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"') . |
103 |
| - ' value="" checked="checked" /><label class="label admin__field-label" for="options_' . |
104 |
| - $_option->getId() . |
105 |
| - '"><span>' . |
106 |
| - __('None') . '</span></label></div>'; |
107 |
| - } |
108 |
| - break; |
109 |
| - case \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_CHECKBOX: |
110 |
| - $type = 'checkbox'; |
111 |
| - $class = 'checkbox admin__control-checkbox'; |
112 |
| - $arraySign = '[]'; |
113 |
| - break; |
114 |
| - } |
115 |
| - $count = 1; |
116 |
| - foreach ($_option->getValues() as $_value) { |
117 |
| - $count++; |
118 |
| - |
119 |
| - $priceStr = $this->_formatPrice( |
120 |
| - [ |
121 |
| - 'is_percent' => $_value->getPriceType() == 'percent', |
122 |
| - 'pricing_value' => $_value->getPrice($_value->getPriceType() == 'percent'), |
123 |
| - ] |
124 |
| - ); |
125 |
| - |
126 |
| - $htmlValue = $_value->getOptionTypeId(); |
127 |
| - if ($arraySign) { |
128 |
| - $checked = is_array($configValue) && in_array($htmlValue, $configValue) ? 'checked' : ''; |
129 |
| - } else { |
130 |
| - $checked = $configValue == $htmlValue ? 'checked' : ''; |
131 |
| - } |
132 |
| - |
133 |
| - $dataSelector = 'options[' . $_option->getId() . ']'; |
134 |
| - if ($arraySign) { |
135 |
| - $dataSelector .= '[' . $htmlValue . ']'; |
136 |
| - } |
137 |
| - |
138 |
| - $selectHtml .= '<div class="field choice admin__field admin__field-option' . |
139 |
| - $require . |
140 |
| - '">' . |
141 |
| - '<input type="' . |
142 |
| - $type . |
143 |
| - '" class="' . |
144 |
| - $class . |
145 |
| - ' ' . |
146 |
| - $require . |
147 |
| - ' product-custom-option"' . |
148 |
| - ($this->getSkipJsReloadPrice() ? '' : ' onclick="opConfig.reloadPrice()"') . |
149 |
| - ' name="options[' . |
150 |
| - $_option->getId() . |
151 |
| - ']' . |
152 |
| - $arraySign . |
153 |
| - '" id="options_' . |
154 |
| - $_option->getId() . |
155 |
| - '_' . |
156 |
| - $count . |
157 |
| - '" value="' . |
158 |
| - $htmlValue . |
159 |
| - '" ' . |
160 |
| - $checked . |
161 |
| - ' data-selector="' . $dataSelector . '"' . |
162 |
| - ' price="' . |
163 |
| - $this->pricingHelper->currencyByStore($_value->getPrice(true), $store, false) . |
164 |
| - '" />' . |
165 |
| - '<label class="label admin__field-label" for="options_' . |
166 |
| - $_option->getId() . |
167 |
| - '_' . |
168 |
| - $count . |
169 |
| - '"><span>' . |
170 |
| - $_value->getTitle() . |
171 |
| - '</span> ' . |
172 |
| - $priceStr . |
173 |
| - '</label>'; |
174 |
| - $selectHtml .= '</div>'; |
175 |
| - } |
176 |
| - $selectHtml .= '</div>'; |
177 |
| - |
178 |
| - return $selectHtml; |
| 76 | + $optionBlock = $this->checkableFactory->create(); |
179 | 77 | }
|
| 78 | + |
| 79 | + return $optionBlock |
| 80 | + ->setOption($option) |
| 81 | + ->setProduct($this->getProduct()) |
| 82 | + ->setSkipJsReloadPrice(1) |
| 83 | + ->_toHtml(); |
180 | 84 | }
|
181 | 85 | }
|
0 commit comments