Skip to content

Commit b02cc2b

Browse files
committed
MC-39134: Custom options date is not populated when editing reorder items
- Fix custom date option is not populated when editing quote item after enabling javascript calendar
1 parent 2cf0433 commit b02cc2b

File tree

2 files changed

+406
-8
lines changed
  • app/code/Magento/Catalog/Block/Product/View/Options/Type
  • dev/tests/integration/testsuite/Magento/Catalog/Block/Product/View/Options/Type

2 files changed

+406
-8
lines changed

app/code/Magento/Catalog/Block/Product/View/Options/Type/Date.php

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
namespace Magento\Catalog\Block\Product\View\Options\Type;
77

8+
use DateTimeZone;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Data\Form\FilterFactory;
11+
use Magento\Framework\Stdlib\DateTime;
12+
813
/**
914
* Product options text type block
1015
*
@@ -27,22 +32,30 @@ class Date extends \Magento\Catalog\Block\Product\View\Options\AbstractOptions
2732
*/
2833
protected $_catalogProductOptionTypeDate;
2934

35+
/**
36+
* @var FilterFactory
37+
*/
38+
private $filterFactory;
39+
3040
/**
3141
* @param \Magento\Framework\View\Element\Template\Context $context
3242
* @param \Magento\Framework\Pricing\Helper\Data $pricingHelper
3343
* @param \Magento\Catalog\Helper\Data $catalogData
3444
* @param \Magento\Catalog\Model\Product\Option\Type\Date $catalogProductOptionTypeDate
3545
* @param array $data
46+
* @param FilterFactory|null $filterFactory
3647
*/
3748
public function __construct(
3849
\Magento\Framework\View\Element\Template\Context $context,
3950
\Magento\Framework\Pricing\Helper\Data $pricingHelper,
4051
\Magento\Catalog\Helper\Data $catalogData,
4152
\Magento\Catalog\Model\Product\Option\Type\Date $catalogProductOptionTypeDate,
42-
array $data = []
53+
array $data = [],
54+
?FilterFactory $filterFactory = null
4355
) {
4456
$this->_catalogProductOptionTypeDate = $catalogProductOptionTypeDate;
4557
parent::__construct($context, $pricingHelper, $catalogData, $data);
58+
$this->filterFactory = $filterFactory ?? ObjectManager::getInstance()->get(FilterFactory::class);
4659
}
4760

4861
/**
@@ -77,14 +90,24 @@ public function getDateHtml()
7790
public function getCalendarDateHtml()
7891
{
7992
$option = $this->getOption();
80-
$value = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId() . '/date');
93+
$values = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId());
8194

8295
$yearStart = $this->_catalogProductOptionTypeDate->getYearStart();
8396
$yearEnd = $this->_catalogProductOptionTypeDate->getYearEnd();
8497

85-
$dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
98+
$dateFormat = $this->_localeDate->getDateFormatWithLongYear();
8699
/** Escape RTL characters which are present in some locales and corrupt formatting */
87100
$escapedDateFormat = preg_replace('/[^MmDdYy\/\.\-]/', '', $dateFormat);
101+
$value = null;
102+
if (is_array($values)) {
103+
$date = $this->getInternalDateString($values);
104+
if ($date !== null) {
105+
$dateFilter = $this->filterFactory->create('date', ['format' => $escapedDateFormat]);
106+
$value = $dateFilter->outputFilter($date);
107+
} elseif (isset($values['date'])) {
108+
$value = $values['date'];
109+
}
110+
}
88111
$calendar = $this->getLayout()->createBlock(
89112
\Magento\Framework\View\Element\Html\Date::class
90113
)->setId(
@@ -158,8 +181,8 @@ public function getTimeHtml()
158181
* Return drop-down html with range of values
159182
*
160183
* @param string $name Id/name of html select element
161-
* @param int $from Start position
162-
* @param int $to End position
184+
* @param int $from Start position
185+
* @param int $to End position
163186
* @param int|null $value Value selected
164187
* @return string Formatted Html
165188
*/
@@ -209,9 +232,8 @@ protected function _getHtmlSelect($name, $value = null)
209232

210233
$select->setExtraParams($extraParams);
211234
if ($value === null) {
212-
$value = $this->getProduct()->getPreconfiguredValues()->getData(
213-
'options/' . $option->getId() . '/' . $name
214-
);
235+
$values = $this->getProduct()->getPreconfiguredValues()->getData('options/' . $option->getId());
236+
$value = is_array($values) ? $this->parseDate($values, $name) : null;
215237
}
216238
if ($value !== null) {
217239
$select->setValue($value);
@@ -233,4 +255,56 @@ protected function _getValueWithLeadingZeros($value)
233255
}
234256
return $value < 10 ? '0' . $value : $value;
235257
}
258+
259+
/**
260+
* Get internal date format of provided value
261+
*
262+
* @param array $value
263+
* @return string|null
264+
*/
265+
private function getInternalDateString(array $value): ?string
266+
{
267+
$result = null;
268+
if (!empty($value['date']) && !empty($value['date_internal'])) {
269+
$dateTimeZone = new DateTimeZone($this->_localeDate->getConfigTimezone());
270+
$dateTimeObject = date_create_from_format(
271+
DateTime::DATETIME_PHP_FORMAT,
272+
$value['date_internal'],
273+
$dateTimeZone
274+
);
275+
if ($dateTimeObject !== false) {
276+
$result = $dateTimeObject->format(DateTime::DATE_PHP_FORMAT);
277+
}
278+
} elseif (!empty($value['day']) && !empty($value['month']) && !empty($value['year'])) {
279+
$dateTimeObject = $this->_localeDate->date();
280+
$dateTimeObject->setDate((int) $value['year'], (int) $value['month'], (int) $value['day']);
281+
$result = $dateTimeObject->format(DateTime::DATE_PHP_FORMAT);
282+
}
283+
return $result;
284+
}
285+
286+
/**
287+
* Parse option value and return the requested part
288+
*
289+
* @param array $value
290+
* @param string $part [year, month, day, hour, minute, day_part]
291+
* @return string|null
292+
*/
293+
private function parseDate(array $value, string $part): ?string
294+
{
295+
$result = null;
296+
if (!empty($value['date']) && !empty($value['date_internal'])) {
297+
$formatDate = explode(' ', $value['date_internal']);
298+
$date = explode('-', $formatDate[0]);
299+
$value['year'] = $date[0];
300+
$value['month'] = $date[1];
301+
$value['day'] = $date[2];
302+
}
303+
304+
if (isset($value[$part])) {
305+
$result = (string) $value[$part];
306+
}
307+
308+
return $result;
309+
}
236310
}

0 commit comments

Comments
 (0)