Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
Save.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\CheckoutAgreements\Model\AgreementFactory;
17 
18 class Save extends Agreement implements HttpPostActionInterface
19 {
23  private $agreementFactory;
24 
30  public function __construct(
31  Context $context,
32  Registry $coreRegistry,
33  AgreementFactory $agreementFactory = null
34  ) {
35  $this->agreementFactory = $agreementFactory ?:
36  ObjectManager::getInstance()->get(AgreementFactory::class);
37  parent::__construct($context, $coreRegistry);
38  }
42  public function execute()
43  {
44  $postData = $this->getRequest()->getPostValue();
45  if ($postData) {
46  $model = $this->agreementFactory->create();
47  $model->setData($postData);
48 
49  try {
50  $validationResult = $model->validateData(new DataObject($postData));
51  if ($validationResult !== true) {
52  foreach ($validationResult as $message) {
53  $this->messageManager->addError($message);
54  }
55  } else {
56  $model->save();
57  $this->messageManager->addSuccess(__('You saved the condition.'));
58  $this->_redirect('checkout/*/');
59  return;
60  }
61  } catch (LocalizedException $e) {
62  $this->messageManager->addError($e->getMessage());
63  } catch (\Exception $e) {
64  $this->messageManager->addError(__('Something went wrong while saving this condition.'));
65  }
66 
67  $this->_session->setAgreementData($postData);
68  $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($this->getUrl('*')));
69  }
70  }
71 }
__()
Definition: __.php:13
$message
__construct(Context $context, Registry $coreRegistry, AgreementFactory $agreementFactory=null)
Definition: Save.php:30