Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Update.php
Go to the documentation of this file.
1 <?php
7 
11 
13 {
17  protected $wishlistProvider;
18 
22  protected $_formKeyValidator;
23 
27  protected $quantityProcessor;
28 
35  public function __construct(
36  Action\Context $context,
37  \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
39  \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor
40  ) {
41  $this->_formKeyValidator = $formKeyValidator;
42  $this->wishlistProvider = $wishlistProvider;
43  $this->quantityProcessor = $quantityProcessor;
44  parent::__construct($context);
45  }
46 
55  public function execute()
56  {
58  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
59  if (!$this->_formKeyValidator->validate($this->getRequest())) {
60  $resultRedirect->setPath('*/*/');
61  return $resultRedirect;
62  }
63  $wishlist = $this->wishlistProvider->getWishlist();
64  if (!$wishlist) {
65  throw new NotFoundException(__('Page not found.'));
66  }
67 
68  $post = $this->getRequest()->getPostValue();
69  if ($post && isset($post['description']) && is_array($post['description'])) {
70  $updatedItems = 0;
71 
72  foreach ($post['description'] as $itemId => $description) {
73  $item = $this->_objectManager->create(\Magento\Wishlist\Model\Item::class)->load($itemId);
74  if ($item->getWishlistId() != $wishlist->getId()) {
75  continue;
76  }
77 
78  // Extract new values
79  $description = (string)$description;
80 
81  if ($description == $this->_objectManager->get(
82  \Magento\Wishlist\Helper\Data::class
83  )->defaultCommentString()
84  ) {
85  $description = '';
86  } elseif (!strlen($description)) {
87  $description = $item->getDescription();
88  }
89 
90  $qty = null;
91  if (isset($post['qty'][$itemId])) {
92  $qty = $this->quantityProcessor->process($post['qty'][$itemId]);
93  }
94  if ($qty === null) {
95  $qty = $item->getQty();
96  if (!$qty) {
97  $qty = 1;
98  }
99  } elseif (0 == $qty) {
100  try {
101  $item->delete();
102  } catch (\Exception $e) {
103  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
104  $this->messageManager->addError(__('We can\'t delete item from Wish List right now.'));
105  }
106  }
107 
108  // Check that we need to save
109  if ($item->getDescription() == $description && $item->getQty() == $qty) {
110  continue;
111  }
112  try {
113  $item->setDescription($description)->setQty($qty)->save();
114  $this->messageManager->addSuccessMessage(
115  __('%1 has been updated in your Wish List.', $item->getProduct()->getName())
116  );
117  $updatedItems++;
118  } catch (\Exception $e) {
119  $this->messageManager->addError(
120  __(
121  'Can\'t save description %1',
122  $this->_objectManager->get(\Magento\Framework\Escaper::class)->escapeHtml($description)
123  )
124  );
125  }
126  }
127 
128  // save wishlist model for setting date of last update
129  if ($updatedItems) {
130  try {
131  $wishlist->save();
132  $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
133  } catch (\Exception $e) {
134  $this->messageManager->addError(__('Can\'t update wish list'));
135  }
136  }
137 
138  if (isset($post['save_and_share'])) {
139  $resultRedirect->setPath('*/*/share', ['wishlist_id' => $wishlist->getId()]);
140  return $resultRedirect;
141  }
142  }
143  $resultRedirect->setPath('*', ['wishlist_id' => $wishlist->getId()]);
144  return $resultRedirect;
145  }
146 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(Action\Context $context, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider, \Magento\Wishlist\Model\LocaleQuantityProcessor $quantityProcessor)
Definition: Update.php:35
__()
Definition: __.php:13
$wishlist
Definition: wishlist.php:10