Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Select.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class Select extends \Magento\Catalog\Model\Product\Option\Type\DefaultType
15 {
20 
24  protected $_escaper;
25 
31  protected $string;
32 
40  public function __construct(
41  \Magento\Checkout\Model\Session $checkoutSession,
42  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
43  \Magento\Framework\Stdlib\StringUtils $string,
44  \Magento\Framework\Escaper $escaper,
45  array $data = []
46  ) {
47  $this->string = $string;
48  $this->_escaper = $escaper;
49  parent::__construct($checkoutSession, $scopeConfig, $data);
50  }
51 
59  public function validateUserValue($values)
60  {
61  parent::validateUserValue($values);
62 
63  $option = $this->getOption();
64  $value = $this->getUserValue();
65 
66  if (empty($value) && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) {
67  $this->setIsValid(false);
68  throw new LocalizedException(
69  __("The product's required option(s) weren't entered. Make sure the options are entered and try again.")
70  );
71  }
72  if (!$this->_isSingleSelection()) {
73  $valuesCollection = $option->getOptionValuesByOptionId($value, $this->getProduct()->getStoreId())->load();
74  $valueCount = is_array($value) ? count($value) : 1;
75  if ($valuesCollection->count() != $valueCount) {
76  $this->setIsValid(false);
77  throw new LocalizedException(
78  __(
79  "The product's required option(s) weren't entered. "
80  . "Make sure the options are entered and try again."
81  )
82  );
83  }
84  }
85  return $this;
86  }
87 
93  public function prepareForCart()
94  {
95  if ($this->getIsValid() && $this->getUserValue()) {
96  return is_array($this->getUserValue()) ? implode(',', $this->getUserValue()) : $this->getUserValue();
97  } else {
98  return null;
99  }
100  }
101 
109  {
110  if ($this->_formattedOptionValue === null) {
111  $this->_formattedOptionValue = $this->_escaper->escapeHtml($this->getEditableOptionValue($optionValue));
112  }
114  }
115 
123  {
124  return $this->getFormattedOptionValue($optionValue);
125  }
126 
132  protected function _getWrongConfigurationMessage()
133  {
134  return __('Some of the selected item options are not currently available.');
135  }
136 
144  {
145  $option = $this->getOption();
146  $result = '';
147  if (!$this->_isSingleSelection()) {
148  foreach (explode(',', $optionValue) as $_value) {
149  $_result = $option->getValueById($_value);
150  if ($_result) {
151  $result .= $_result->getTitle() . ', ';
152  } else {
153  if ($this->getListener()) {
154  $this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage());
155  $result = '';
156  break;
157  }
158  }
159  }
160  $result = $this->string->substr($result, 0, -2);
161  } elseif ($this->_isSingleSelection()) {
162  $_result = $option->getValueById($optionValue);
163  if ($_result) {
164  $result = $_result->getTitle();
165  } else {
166  if ($this->getListener()) {
167  $this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage());
168  }
169  $result = '';
170  }
171  } else {
173  }
174  return $result;
175  }
176 
184  public function parseOptionValue($optionValue, $productOptionValues)
185  {
186  $values = [];
187  if (!$this->_isSingleSelection()) {
188  foreach (explode(',', $optionValue) as $value) {
189  $value = trim($value);
190  if (array_key_exists($value, $productOptionValues)) {
191  $values[] = $productOptionValues[$value];
192  }
193  }
194  } elseif ($this->_isSingleSelection() && array_key_exists($optionValue, $productOptionValues)) {
195  $values[] = $productOptionValues[$optionValue];
196  }
197  if (count($values)) {
198  return implode(',', $values);
199  } else {
200  return null;
201  }
202  }
203 
211  {
212  if (!$this->_isSingleSelection()) {
213  return explode(',', $optionValue);
214  }
215  return $optionValue;
216  }
217 
225  public function getOptionPrice($optionValue, $basePrice)
226  {
227  $option = $this->getOption();
228  $result = 0;
229 
230  if (!$this->_isSingleSelection()) {
231  foreach (explode(',', $optionValue) as $value) {
232  $_result = $option->getValueById($value);
233  if ($_result) {
235  $_result->getPrice(),
236  $_result->getPriceType() == 'percent',
237  $basePrice
238  );
239  } else {
240  if ($this->getListener()) {
241  $this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage());
242  break;
243  }
244  }
245  }
246  } elseif ($this->_isSingleSelection()) {
247  $_result = $option->getValueById($optionValue);
248  if ($_result) {
250  $_result->getPrice(),
251  $_result->getPriceType() == 'percent',
252  $basePrice
253  );
254  } else {
255  if ($this->getListener()) {
256  $this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage());
257  }
258  }
259  }
260 
261  return $result;
262  }
263 
271  public function getOptionSku($optionValue, $skuDelimiter)
272  {
273  $option = $this->getOption();
274 
275  if (!$this->_isSingleSelection()) {
276  $skus = [];
277  foreach (explode(',', $optionValue) as $value) {
278  $optionSku = $option->getValueById($value);
279  if ($optionSku) {
280  $skus[] = $optionSku->getSku();
281  } else {
282  if ($this->getListener()) {
283  $this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage());
284  break;
285  }
286  }
287  }
288  $result = implode($skuDelimiter, $skus);
289  } elseif ($this->_isSingleSelection()) {
290  $result = $option->getValueById($optionValue);
291  if ($result) {
292  return $result->getSku();
293  } else {
294  if ($this->getListener()) {
295  $this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage());
296  }
297  return '';
298  }
299  } else {
300  $result = parent::getOptionSku($optionValue, $skuDelimiter);
301  }
302 
303  return $result;
304  }
305 
311  protected function _isSingleSelection()
312  {
313  $single = [
316  ];
317  return in_array($this->getOption()->getType(), $single);
318  }
319 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
foreach($websiteCodes as $websiteCode) $skus
$value
Definition: gender.phtml:16
getOptionSku($optionValue, $skuDelimiter)
Definition: Select.php:271
parseOptionValue($optionValue, $productOptionValues)
Definition: Select.php:184
getOptionPrice($optionValue, $basePrice)
Definition: Select.php:225
_getChargeableOptionPrice($price, $isPercent, $basePrice)
$_value
Definition: tax.phtml:15
__construct(\Magento\Checkout\Model\Session $checkoutSession, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Stdlib\StringUtils $string, \Magento\Framework\Escaper $escaper, array $data=[])
Definition: Select.php:40