Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
10 use Magento\Customer\Api\Data\GroupInterfaceFactory;
13 
15 {
20 
31  public function __construct(
32  \Magento\Backend\App\Action\Context $context,
33  \Magento\Framework\Registry $coreRegistry,
35  GroupInterfaceFactory $groupDataFactory,
36  \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
37  \Magento\Framework\View\Result\PageFactory $resultPageFactory,
38  \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
39  ) {
40  $this->dataObjectProcessor = $dataObjectProcessor;
41  parent::__construct(
42  $context,
43  $coreRegistry,
48  );
49  }
50 
57  protected function storeCustomerGroupDataToSession($customerGroupData)
58  {
59  if (array_key_exists('code', $customerGroupData)) {
60  $customerGroupData['customer_group_code'] = $customerGroupData['code'];
61  unset($customerGroupData['code']);
62  }
63  $this->_getSession()->setCustomerGroupData($customerGroupData);
64  }
65 
71  public function execute()
72  {
73  $taxClass = (int)$this->getRequest()->getParam('tax_class');
74 
76  $customerGroup = null;
77  if ($taxClass) {
78  $id = $this->getRequest()->getParam('id');
79  $resultRedirect = $this->resultRedirectFactory->create();
80  try {
81  $customerGroupCode = (string)$this->getRequest()->getParam('code');
82  if ($id !== null) {
83  $customerGroup = $this->groupRepository->getById((int)$id);
84  $customerGroupCode = $customerGroupCode ?: $customerGroup->getCode();
85  } else {
86  $customerGroup = $this->groupDataFactory->create();
87  }
88  $customerGroup->setCode(!empty($customerGroupCode) ? $customerGroupCode : null);
89  $customerGroup->setTaxClassId($taxClass);
90 
91  $this->groupRepository->save($customerGroup);
92 
93  $this->messageManager->addSuccess(__('You saved the customer group.'));
94  $resultRedirect->setPath('customer/group');
95  } catch (\Exception $e) {
96  $this->messageManager->addError($e->getMessage());
97  if ($customerGroup != null) {
99  $this->dataObjectProcessor->buildOutputDataArray(
100  $customerGroup,
101  \Magento\Customer\Api\Data\GroupInterface::class
102  )
103  );
104  }
105  $resultRedirect->setPath('customer/group/edit', ['id' => $id]);
106  }
107  return $resultRedirect;
108  } else {
109  return $this->resultForwardFactory->create()->forward('new');
110  }
111  }
112 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
storeCustomerGroupDataToSession($customerGroupData)
Definition: Save.php:57
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Framework\Registry $coreRegistry, GroupRepositoryInterface $groupRepository, GroupInterfaceFactory $groupDataFactory, \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor)
Definition: Save.php:31