Skip to content

Commit 3b761f1

Browse files
authored
ENGCOM-4215: Entered data missing when entering the wrong date for from, to in cart rule #20895
2 parents a41c3ea + 97c853b commit 3b761f1

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

app/code/Magento/SalesRule/Controller/Adminhtml/Promo/Quote/Save.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
88

99
use Magento\Framework\App\Action\HttpPostActionInterface;
10+
use Magento\Framework\App\Request\DataPersistorInterface;
1011
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1112

1213
/**
@@ -20,25 +21,35 @@ class Save extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote implement
2021
* @var TimezoneInterface
2122
*/
2223
private $timezone;
24+
25+
/**
26+
* @var DataPersistorInterface
27+
*/
28+
private $dataPersistor;
2329

2430
/**
2531
* @param \Magento\Backend\App\Action\Context $context
2632
* @param \Magento\Framework\Registry $coreRegistry
2733
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
2834
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
2935
* @param TimezoneInterface $timezone
36+
* @param DataPersistorInterface $dataPersistor
3037
*/
3138
public function __construct(
3239
\Magento\Backend\App\Action\Context $context,
3340
\Magento\Framework\Registry $coreRegistry,
3441
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
3542
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
36-
TimezoneInterface $timezone = null
43+
TimezoneInterface $timezone = null,
44+
DataPersistorInterface $dataPersistor = null
3745
) {
3846
parent::__construct($context, $coreRegistry, $fileFactory, $dateFilter);
3947
$this->timezone = $timezone ?? \Magento\Framework\App\ObjectManager::getInstance()->get(
4048
TimezoneInterface::class
4149
);
50+
$this->dataPersistor = $dataPersistor ?? \Magento\Framework\App\ObjectManager::getInstance()->get(
51+
DataPersistorInterface::class
52+
);
4253
}
4354

4455
/**
@@ -73,12 +84,8 @@ public function execute()
7384
$data
7485
);
7586
$data = $inputFilter->getUnescaped();
76-
$id = $this->getRequest()->getParam('rule_id');
77-
if ($id) {
78-
$model->load($id);
79-
if ($id != $model->getId()) {
80-
throw new \Magento\Framework\Exception\LocalizedException(__('The wrong rule is specified.'));
81-
}
87+
if (!$this->checkRuleExists($model)) {
88+
throw new \Magento\Framework\Exception\LocalizedException(__('The wrong rule is specified.'));
8289
}
8390

8491
$session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
@@ -89,6 +96,7 @@ public function execute()
8996
$this->messageManager->addErrorMessage($errorMessage);
9097
}
9198
$session->setPageData($data);
99+
$this->dataPersistor->set('sale_rule', $data);
92100
$this->_redirect('sales_rule/*/edit', ['id' => $model->getId()]);
93101
return;
94102
}
@@ -147,4 +155,22 @@ public function execute()
147155
}
148156
$this->_redirect('sales_rule/*/');
149157
}
158+
159+
/**
160+
* Check if Cart Price Rule with provided id exists.
161+
*
162+
* @param \Magento\SalesRule\Model\Rule $model
163+
* @return bool
164+
*/
165+
private function checkRuleExists(\Magento\SalesRule\Model\Rule $model): bool
166+
{
167+
$id = $this->getRequest()->getParam('rule_id');
168+
if ($id) {
169+
$model->load($id);
170+
if ($model->getId() != $id) {
171+
return false;
172+
}
173+
}
174+
return true;
175+
}
150176
}

app/code/Magento/SalesRule/Model/Rule/DataProvider.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\SalesRule\Model\Rule;
77

8+
use Magento\Framework\App\Request\DataPersistorInterface;
89
use Magento\SalesRule\Model\ResourceModel\Rule\Collection;
910
use Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory;
1011
use Magento\SalesRule\Model\Rule;
@@ -36,6 +37,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
3637
*/
3738
protected $metadataValueProvider;
3839

40+
/**
41+
* @var DataPersistorInterface
42+
*/
43+
private $dataPersistor;
44+
3945
/**
4046
* Initialize dependencies.
4147
*
@@ -47,6 +53,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
4753
* @param Metadata\ValueProvider $metadataValueProvider
4854
* @param array $meta
4955
* @param array $data
56+
* @param DataPersistorInterface $dataPersistor
5057
*/
5158
public function __construct(
5259
$name,
@@ -56,12 +63,16 @@ public function __construct(
5663
\Magento\Framework\Registry $registry,
5764
\Magento\SalesRule\Model\Rule\Metadata\ValueProvider $metadataValueProvider,
5865
array $meta = [],
59-
array $data = []
66+
array $data = [],
67+
DataPersistorInterface $dataPersistor = null
6068
) {
6169
$this->collection = $collectionFactory->create();
6270
$this->coreRegistry = $registry;
6371
$this->metadataValueProvider = $metadataValueProvider;
6472
$meta = array_replace_recursive($this->getMetadataValues(), $meta);
73+
$this->dataPersistor = $dataPersistor ?? \Magento\Framework\App\ObjectManager::getInstance()->get(
74+
DataPersistorInterface::class
75+
);
6576
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
6677
}
6778

@@ -77,7 +88,7 @@ protected function getMetadataValues()
7788
}
7889

7990
/**
80-
* {@inheritdoc}
91+
* @inheritdoc
8192
*/
8293
public function getData()
8394
{
@@ -93,6 +104,13 @@ public function getData()
93104

94105
$this->loadedData[$rule->getId()] = $rule->getData();
95106
}
107+
$data = $this->dataPersistor->get('sale_rule');
108+
if (!empty($data)) {
109+
$rule = $this->collection->getNewEmptyItem();
110+
$rule->setData($data);
111+
$this->loadedData[$rule->getId()] = $rule->getData();
112+
$this->dataPersistor->clear('sale_rule');
113+
}
96114

97115
return $this->loadedData;
98116
}

0 commit comments

Comments
 (0)