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
7 
14 {
18  protected $_saved = false;
19 
25  protected $_giftMessageMessage = null;
26 
30  protected $_session;
31 
35  protected $_messageFactory;
36 
40  protected $productRepository;
41 
48  public function __construct(
49  \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
50  \Magento\GiftMessage\Model\MessageFactory $messageFactory,
51  \Magento\Backend\Model\Session\Quote $session,
52  \Magento\GiftMessage\Helper\Message $giftMessageMessage
53  ) {
54  $this->productRepository = $productRepository;
55  $this->_messageFactory = $messageFactory;
56  $this->_session = $session;
57  $this->_giftMessageMessage = $giftMessageMessage;
58  }
59 
65  public function saveAllInQuote()
66  {
67  $giftmessages = $this->getGiftmessages();
68 
69  if (!is_array($giftmessages)) {
70  return $this;
71  }
72 
73  foreach ($giftmessages as $entityId => $giftmessage) {
74  $entityType = $this->getMappedType($giftmessage['type']);
75  $this->_saveOne($entityId, $giftmessage, $entityType);
76  }
77 
78  return $this;
79  }
80 
86  public function getSaved()
87  {
88  return $this->_saved;
89  }
90 
94  public function saveAllInOrder()
95  {
96  $giftMessages = $this->getGiftmessages();
97 
98  if (!is_array($giftMessages)) {
99  return $this;
100  }
101 
102  foreach ($giftMessages as $entityId => $giftMessage) {
103  $entityType = $this->getMappedType($giftMessage['type']);
104  $this->_saveOne($entityId, $giftMessage, $entityType);
105  }
106 
107  return $this;
108  }
109 
118  protected function _saveOne($entityId, $giftmessage, $entityType)
119  {
120  /* @var $giftmessageModel \Magento\GiftMessage\Model\Message */
121  $giftmessageModel = $this->_messageFactory->create();
122 
123  switch ($entityType) {
124  case 'quote':
125  $entityModel = $this->_getQuote();
126  break;
127 
128  case 'quote_item':
129  $entityModel = $this->_getQuote()->getItemById($entityId);
130  break;
131 
132  default:
133  $entityModel = $giftmessageModel->getEntityModelByType($entityType)->load($entityId);
134  break;
135  }
136 
137  if (!$entityModel) {
138  return $this;
139  }
140 
141  if ($entityModel->getGiftMessageId()) {
142  $giftmessageModel->load($entityModel->getGiftMessageId());
143  }
144 
145  $giftmessageModel->addData($giftmessage);
146 
147  if ($giftmessageModel->isMessageEmpty() && $giftmessageModel->getId()) {
148  // remove empty giftmessage
149  $this->_deleteOne($entityModel, $giftmessageModel);
150  $this->_saved = false;
151  } elseif (!$giftmessageModel->isMessageEmpty()) {
152  $giftmessageModel->save();
153  $entityModel->setGiftMessageId($giftmessageModel->getId());
154  if ($entityType != 'quote') {
155  $entityModel->save();
156  }
157  $this->_saved = true;
158  }
159 
160  return $this;
161  }
162 
170  protected function _deleteOne($entityModel, $giftmessageModel = null)
171  {
172  if ($giftmessageModel === null) {
173  $giftmessageModel = $this->_messageFactory->create()->load($entityModel->getGiftMessageId());
174  }
175  $giftmessageModel->delete();
176  $entityModel->setGiftMessageId(0)->save();
177  return $this;
178  }
179 
186  public function setAllowQuoteItems($items)
187  {
188  $this->_session->setAllowQuoteItemsGiftMessage($items);
189  return $this;
190  }
191 
198  public function addAllowQuoteItem($item)
199  {
200  $items = $this->getAllowQuoteItems();
201  if (!in_array($item, $items)) {
202  $items[] = $item;
203  }
204  $this->setAllowQuoteItems($items);
205 
206  return $this;
207  }
208 
214  public function getAllowQuoteItems()
215  {
216  if (!is_array($this->_session->getAllowQuoteItemsGiftMessage())) {
217  $this->setAllowQuoteItems([]);
218  }
219 
220  return $this->_session->getAllowQuoteItemsGiftMessage();
221  }
222 
228  public function getAllowQuoteItemsProducts()
229  {
230  $result = [];
231  foreach ($this->getAllowQuoteItems() as $itemId) {
232  $item = $this->_getQuote()->getItemById($itemId);
233  if (!$item) {
234  continue;
235  }
236  $result[] = $item->getProduct()->getId();
237  }
238  return $result;
239  }
240 
248  public function getIsAllowedQuoteItem($item)
249  {
250  if (!in_array($item->getId(), $this->getAllowQuoteItems())) {
251  if ($item->getGiftMessageId() && $this->isGiftMessagesAvailable($item)) {
252  $this->addAllowQuoteItem($item->getId());
253  return true;
254  }
255  return false;
256  }
257 
258  return true;
259  }
260 
268  {
269  return $this->_giftMessageMessage->isMessagesAllowed('item', $item, $item->getStore());
270  }
271 
279  {
280  $allowedItems = $this->getAllowQuoteItems();
281  $deleteAllowedItems = [];
282  foreach ($products as $productId => $data) {
283  $product = $this->productRepository->getById($productId, false, $this->_session->getStore()->getId());
284  $item = $this->_getQuote()->getItemByProduct($product);
285 
286  if (!$item) {
287  continue;
288  }
289 
290  if (in_array($item->getId(), $allowedItems) && !isset($data['giftmessage'])) {
291  $deleteAllowedItems[] = $item->getId();
292  } elseif (!in_array($item->getId(), $allowedItems) && isset($data['giftmessage'])) {
293  $allowedItems[] = $item->getId();
294  }
295  }
296 
297  $allowedItems = array_diff($allowedItems, $deleteAllowedItems);
298 
299  $this->setAllowQuoteItems($allowedItems);
300  return $this;
301  }
302 
308  {
309  $allowedItems = $this->getAllowQuoteItems();
310  $deleteAllowedItems = [];
311  foreach ($items as $itemId => $data) {
312  $item = $this->_getQuote()->getItemById($itemId);
313 
314  if (!$item) {
315  // Clean not exists items
316  $deleteAllowedItems[] = $itemId;
317  continue;
318  }
319 
320  if (in_array($item->getId(), $allowedItems) && !isset($data['giftmessage'])) {
321  $deleteAllowedItems[] = $item->getId();
322  } elseif (!in_array($item->getId(), $allowedItems) && isset($data['giftmessage'])) {
323  $allowedItems[] = $item->getId();
324  }
325  }
326 
327  $allowedItems = array_diff($allowedItems, $deleteAllowedItems);
328  $this->setAllowQuoteItems($allowedItems);
329  return $this;
330  }
331 
338  protected function getMappedType($type)
339  {
340  $map = [
341  'main' => 'quote',
342  'item' => 'quote_item',
343  'order' => 'order',
344  'order_item' => 'order_item',
345  ];
346 
347  if (isset($map[$type])) {
348  return $map[$type];
349  }
350  return null;
351  }
352 
359  protected function _getQuote()
360  {
361  return $this->_session->getQuote();
362  }
363 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$giftMessage
Definition: items.phtml:47
importAllowQuoteItemsFromProducts($products)
Definition: Save.php:278
_deleteOne($entityModel, $giftmessageModel=null)
Definition: Save.php:170
_saveOne($entityId, $giftmessage, $entityType)
Definition: Save.php:118
$type
Definition: item.phtml:13
__construct(\Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\GiftMessage\Model\MessageFactory $messageFactory, \Magento\Backend\Model\Session\Quote $session, \Magento\GiftMessage\Helper\Message $giftMessageMessage)
Definition: Save.php:48
importAllowQuoteItemsFromItems($items)
Definition: Save.php:307
$messageFactory
Definition: messages.php:10
$items