Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Create.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Backend\Model\View\Result\ForwardFactory;
11 
18 abstract class Create extends \Magento\Backend\App\Action
19 {
23  protected $escaper;
24 
28  protected $resultPageFactory;
29 
34 
42  public function __construct(
43  Action\Context $context,
44  \Magento\Catalog\Helper\Product $productHelper,
45  \Magento\Framework\Escaper $escaper,
47  ForwardFactory $resultForwardFactory
48  ) {
49  parent::__construct($context);
50  $productHelper->setSkipSaleableCheck(true);
51  $this->escaper = $escaper;
52  $this->resultPageFactory = $resultPageFactory;
53  $this->resultForwardFactory = $resultForwardFactory;
54  }
55 
61  protected function _getSession()
62  {
63  return $this->_objectManager->get(\Magento\Backend\Model\Session\Quote::class);
64  }
65 
71  protected function _getQuote()
72  {
73  return $this->_getSession()->getQuote();
74  }
75 
81  protected function _getOrderCreateModel()
82  {
83  return $this->_objectManager->get(\Magento\Sales\Model\AdminOrder\Create::class);
84  }
85 
91  protected function _getGiftmessageSaveModel()
92  {
93  return $this->_objectManager->get(\Magento\GiftMessage\Model\Save::class);
94  }
95 
101  protected function _initSession()
102  {
106  if ($customerId = $this->getRequest()->getParam('customer_id')) {
107  $this->_getSession()->setCustomerId((int)$customerId);
108  }
109 
113  if ($storeId = $this->getRequest()->getParam('store_id')) {
114  $this->_getSession()->setStoreId((int)$storeId);
115  }
116 
120  if ($currencyId = $this->getRequest()->getParam('currency_id')) {
121  $this->_getSession()->setCurrencyId((string)$currencyId);
122  $this->_getOrderCreateModel()->setRecollect(true);
123  }
124  return $this;
125  }
126 
132  protected function _processData()
133  {
134  return $this->_processActionData();
135  }
136 
146  protected function _processActionData($action = null)
147  {
148  $eventData = [
149  'order_create_model' => $this->_getOrderCreateModel(),
150  'request_model' => $this->getRequest(),
151  'session' => $this->_getSession(),
152  ];
153 
154  $this->_eventManager->dispatch('adminhtml_sales_order_create_process_data_before', $eventData);
155 
159  if ($data = $this->getRequest()->getPost('order')) {
160  $this->_getOrderCreateModel()->importPostData($data);
161  }
162 
166  $this->_getOrderCreateModel()->initRuleData();
167 
171  $this->_getOrderCreateModel()->getBillingAddress();
172 
176  if (!$this->_getOrderCreateModel()->getQuote()->isVirtual()) {
177  $syncFlag = $this->getRequest()->getPost('shipping_as_billing');
178  $shippingMethod = $this->_getOrderCreateModel()->getShippingAddress()->getShippingMethod();
179  if ($syncFlag === null
180  && $this->_getOrderCreateModel()->getShippingAddress()->getSameAsBilling() && empty($shippingMethod)
181  ) {
182  $this->_getOrderCreateModel()->setShippingAsBilling(1);
183  } else {
184  $this->_getOrderCreateModel()->setShippingAsBilling((int)$syncFlag);
185  }
186  }
187 
191  if (!$this->_getOrderCreateModel()->getQuote()->isVirtual() && $this->getRequest()->getPost('reset_shipping')
192  ) {
193  $this->_getOrderCreateModel()->resetShippingMethod(true);
194  }
195 
199  if (!$this->_getOrderCreateModel()->getQuote()->isVirtual() && $this->getRequest()->getPost(
200  'collect_shipping_rates'
201  )
202  ) {
203  $this->_getOrderCreateModel()->collectShippingRates();
204  }
205 
209  if ($data = $this->getRequest()->getPost('sidebar')) {
210  $this->_getOrderCreateModel()->applySidebarData($data);
211  }
212 
213  $this->_eventManager->dispatch('adminhtml_sales_order_create_process_item_before', $eventData);
214 
218  if ($productId = (int)$this->getRequest()->getPost('add_product')) {
219  $this->_getOrderCreateModel()->addProduct($productId, $this->getRequest()->getPostValue());
220  }
221 
225  if ($this->getRequest()->has('item') && !$this->getRequest()->getPost('update_items') && !($action == 'save')
226  ) {
227  $items = $this->getRequest()->getPost('item');
228  $items = $this->_processFiles($items);
229  $this->_getOrderCreateModel()->addProducts($items);
230  }
231 
235  if ($this->getRequest()->getPost('update_items')) {
236  $items = $this->getRequest()->getPost('item', []);
237  $items = $this->_processFiles($items);
238  $this->_getOrderCreateModel()->updateQuoteItems($items);
239  }
240 
244  $removeItemId = (int)$this->getRequest()->getPost('remove_item');
245  $removeFrom = (string)$this->getRequest()->getPost('from');
246  if ($removeItemId && $removeFrom) {
247  $this->_getOrderCreateModel()->removeItem($removeItemId, $removeFrom);
248  $this->_getOrderCreateModel()->recollectCart();
249  }
250 
254  $moveItemId = (int)$this->getRequest()->getPost('move_item');
255  $moveTo = (string)$this->getRequest()->getPost('to');
256  $moveQty = (int)$this->getRequest()->getPost('qty');
257  if ($moveItemId && $moveTo) {
258  $this->_getOrderCreateModel()->moveQuoteItem($moveItemId, $moveTo, $moveQty);
259  }
260 
261  $this->_eventManager->dispatch('adminhtml_sales_order_create_process_item_after', $eventData);
262 
263  if ($paymentData = $this->getRequest()->getPost('payment')) {
264  $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentData);
265  }
266 
267  $eventData = [
268  'order_create_model' => $this->_getOrderCreateModel(),
269  'request' => $this->getRequest()->getPostValue(),
270  ];
271 
272  $this->_eventManager->dispatch('adminhtml_sales_order_create_process_data', $eventData);
273 
274  $this->_getOrderCreateModel()->saveQuote();
275 
276  if ($paymentData = $this->getRequest()->getPost('payment')) {
277  $this->_getOrderCreateModel()->getQuote()->getPayment()->addData($paymentData);
278  }
279 
283  $giftmessages = $this->getRequest()->getPost('giftmessage');
284  if ($giftmessages) {
285  $this->_getGiftmessageSaveModel()->setGiftmessages($giftmessages)->saveAllInQuote();
286  }
287 
291  if ($data = $this->getRequest()->getPost('add_products')) {
292  $this->_getGiftmessageSaveModel()->importAllowQuoteItemsFromProducts(
293  $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonDecode($data)
294  );
295  }
296 
300  if ($this->getRequest()->getPost('update_items')) {
301  $items = $this->getRequest()->getPost('item', []);
302  $this->_getGiftmessageSaveModel()->importAllowQuoteItemsFromItems($items);
303  }
304 
305  $data = $this->getRequest()->getPost('order');
306  $couponCode = '';
307  if (isset($data) && isset($data['coupon']['code'])) {
308  $couponCode = trim($data['coupon']['code']);
309  }
310 
311  if (!empty($couponCode)) {
312  $isApplyDiscount = false;
313  foreach ($this->_getQuote()->getAllItems() as $item) {
314  if (!$item->getNoDiscount()) {
315  $isApplyDiscount = true;
316  break;
317  }
318  }
319  if (!$isApplyDiscount) {
320  $this->messageManager->addErrorMessage(
321  __(
322  '"%1" coupon code was not applied. Do not apply discount is selected for item(s)',
323  $this->escaper->escapeHtml($couponCode)
324  )
325  );
326  } else {
327  if ($this->_getQuote()->getCouponCode() !== $couponCode) {
328  $this->messageManager->addErrorMessage(
329  __(
330  'The "%1" coupon code isn\'t valid. Verify the code and try again.',
331  $this->escaper->escapeHtml($couponCode)
332  )
333  );
334  } else {
335  $this->messageManager->addSuccessMessage(__('The coupon code has been accepted.'));
336  }
337  }
338  }
339 
340  return $this;
341  }
342 
349  protected function _processFiles($items)
350  {
351  /* @var $productHelper \Magento\Catalog\Helper\Product */
352  $productHelper = $this->_objectManager->get(\Magento\Catalog\Helper\Product::class);
353  foreach ($items as $id => $item) {
354  $buyRequest = new \Magento\Framework\DataObject($item);
355  $params = ['files_prefix' => 'item_' . $id . '_'];
356  $buyRequest = $productHelper->addParamsToBuyRequest($buyRequest, $params);
357  if ($buyRequest->hasData()) {
358  $items[$id] = $buyRequest->toArray();
359  }
360  }
361  return $items;
362  }
363 
367  protected function _reloadQuote()
368  {
369  $id = $this->_getQuote()->getId();
370  $this->_getQuote()->load($id);
371  return $this;
372  }
373 
379  protected function _isAllowed()
380  {
381  return $this->_authorization->isAllowed($this->_getAclResource());
382  }
383 
389  protected function _getAclResource()
390  {
391  $action = strtolower($this->getRequest()->getActionName());
392  if (in_array($action, ['index', 'save', 'cancel']) && $this->_getSession()->getReordered()) {
393  $action = 'reorder';
394  }
395  switch ($action) {
396  case 'index':
397  case 'save':
398  $aclResource = 'Magento_Sales::create';
399  break;
400  case 'reorder':
401  $aclResource = 'Magento_Sales::reorder';
402  break;
403  case 'cancel':
404  $aclResource = 'Magento_Sales::cancel';
405  break;
406  default:
407  $aclResource = 'Magento_Sales::actions';
408  break;
409  }
410  return $aclResource;
411  }
412 }
__construct(Action\Context $context, \Magento\Catalog\Helper\Product $productHelper, \Magento\Framework\Escaper $escaper, PageFactory $resultPageFactory, ForwardFactory $resultForwardFactory)
Definition: Create.php:42
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest
$shippingMethod
Definition: popup.phtml:12
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$items