Skip to content

Commit e6bc509

Browse files
author
Sergey Semenov
committed
MAGETWO-37742: It's impossible to add Product to Shopping Cart from shared Wishlist
1 parent 183254f commit e6bc509

File tree

12 files changed

+1383
-119
lines changed

12 files changed

+1383
-119
lines changed

app/code/Magento/Wishlist/Block/AbstractBlock.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ public function getSharedItemAddToCartUrl($item)
148148
return $this->_getHelper()->getSharedAddToCartUrl($item);
149149
}
150150

151+
/**
152+
* Retrieve URL for adding All items to shopping cart from shared wishlist
153+
*
154+
* @return string
155+
*/
156+
public function getSharedAddAllToCartUrl()
157+
{
158+
return $this->_getHelper()->getSharedAddAllToCartUrl();
159+
}
160+
151161
/**
152162
* Retrieve params for adding Product to wishlist
153163
*

app/code/Magento/Wishlist/Controller/Index/Send.php

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
namespace Magento\Wishlist\Controller\Index;
88

99
use Magento\Framework\App\Action;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
1011
use Magento\Framework\Exception\NotFoundException;
12+
use Magento\Framework\Session\Generic as WishlistSession;
13+
use Magento\Store\Model\StoreManagerInterface;
1114
use Magento\Wishlist\Controller\IndexInterface;
1215
use Magento\Framework\Controller\ResultFactory;
1316
use Magento\Framework\View\Result\Layout as ResultLayout;
@@ -52,6 +55,21 @@ class Send extends Action\Action implements IndexInterface
5255
*/
5356
protected $_formKeyValidator;
5457

58+
/**
59+
* @var WishlistSession
60+
*/
61+
protected $wishlistSession;
62+
63+
/**
64+
* @var ScopeConfigInterface
65+
*/
66+
protected $scopeConfig;
67+
68+
/**
69+
* @var StoreManagerInterface
70+
*/
71+
protected $storeManager;
72+
5573
/**
5674
* @param Action\Context $context
5775
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
@@ -70,7 +88,10 @@ public function __construct(
7088
\Magento\Wishlist\Model\Config $wishlistConfig,
7189
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
7290
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
73-
\Magento\Customer\Helper\View $customerHelperView
91+
\Magento\Customer\Helper\View $customerHelperView,
92+
WishlistSession $wishlistSession,
93+
ScopeConfigInterface $scopeConfig,
94+
StoreManagerInterface $storeManager
7495
) {
7596
$this->_formKeyValidator = $formKeyValidator;
7697
$this->_customerSession = $customerSession;
@@ -79,6 +100,9 @@ public function __construct(
79100
$this->_transportBuilder = $transportBuilder;
80101
$this->inlineTranslation = $inlineTranslation;
81102
$this->_customerHelperView = $customerHelperView;
103+
$this->wishlistSession = $wishlistSession;
104+
$this->scopeConfig = $scopeConfig;
105+
$this->storeManager = $storeManager;
82106
parent::__construct($context);
83107
}
84108

@@ -108,7 +132,10 @@ public function execute()
108132
$sharingLimit = $this->_wishlistConfig->getSharingEmailLimit();
109133
$textLimit = $this->_wishlistConfig->getSharingTextLimit();
110134
$emailsLeft = $sharingLimit - $wishlist->getShared();
111-
$emails = explode(',', $this->getRequest()->getPost('emails'));
135+
136+
$emails = $this->getRequest()->getPost('emails');
137+
$emails = empty($emails) ? $emails : explode(',', $emails);
138+
112139
$error = false;
113140
$message = (string)$this->getRequest()->getPost('message');
114141
if (strlen($message) > $textLimit) {
@@ -135,11 +162,7 @@ public function execute()
135162

136163
if ($error) {
137164
$this->messageManager->addError($error);
138-
$this->_objectManager->get(
139-
'Magento\Wishlist\Model\Session'
140-
)->setSharingForm(
141-
$this->getRequest()->getPostValue()
142-
);
165+
$this->wishlistSession->setSharingForm($this->getRequest()->getPostValue());
143166
$resultRedirect->setPath('*/*/share');
144167
return $resultRedirect;
145168
}
@@ -159,32 +182,29 @@ public function execute()
159182
$sharingCode = $wishlist->getSharingCode();
160183

161184
try {
162-
$scopeConfig = $this->_objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface');
163-
$storeManager = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface');
164185
foreach ($emails as $email) {
165186
$transport = $this->_transportBuilder->setTemplateIdentifier(
166-
$scopeConfig->getValue(
187+
$this->scopeConfig->getValue(
167188
'wishlist/email/email_template',
168189
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
169190
)
170191
)->setTemplateOptions(
171192
[
172193
'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
173-
'store' => $storeManager->getStore()->getStoreId(),
194+
'store' => $this->storeManager->getStore()->getStoreId(),
174195
]
175196
)->setTemplateVars(
176197
[
177198
'customer' => $customer,
178199
'customerName' => $customerName,
179200
'salable' => $wishlist->isSalable() ? 'yes' : '',
180201
'items' => $this->getWishlistItems($resultLayout),
181-
'addAllLink' => $this->_url->getUrl('*/shared/allcart', ['code' => $sharingCode]),
182202
'viewOnSiteLink' => $this->_url->getUrl('*/shared/index', ['code' => $sharingCode]),
183203
'message' => $message,
184-
'store' => $storeManager->getStore(),
204+
'store' => $this->storeManager->getStore(),
185205
]
186206
)->setFrom(
187-
$scopeConfig->getValue(
207+
$this->scopeConfig->getValue(
188208
'wishlist/email/email_identity',
189209
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
190210
)
@@ -213,11 +233,7 @@ public function execute()
213233
} catch (\Exception $e) {
214234
$this->inlineTranslation->resume();
215235
$this->messageManager->addError($e->getMessage());
216-
$this->_objectManager->get(
217-
'Magento\Wishlist\Model\Session'
218-
)->setSharingForm(
219-
$this->getRequest()->getPostValue()
220-
);
236+
$this->wishlistSession->setSharingForm($this->getRequest()->getPostValue());
221237
$resultRedirect->setPath('*/*/share');
222238
return $resultRedirect;
223239
}

app/code/Magento/Wishlist/Controller/Shared/Cart.php

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,68 @@
55
*/
66
namespace Magento\Wishlist\Controller\Shared;
77

8+
use Magento\Checkout\Helper\Cart as CartHelper;
9+
use Magento\Checkout\Model\Cart as CustomerCart;
10+
use Magento\Framework\App\Action\Context as ActionContext;
811
use Magento\Framework\Controller\ResultFactory;
12+
use Magento\Framework\Escaper;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Wishlist\Model\Item;
15+
use Magento\Wishlist\Model\Item\OptionFactory;
16+
use Magento\Wishlist\Model\ItemFactory;
17+
use Magento\Wishlist\Model\Resource\Item\Option\Collection as OptionCollection;
918

1019
class Cart extends \Magento\Framework\App\Action\Action
1120
{
21+
/**
22+
* @var CustomerCart
23+
*/
24+
protected $cart;
25+
26+
/**
27+
* @var OptionFactory
28+
*/
29+
protected $optionFactory;
30+
31+
/**
32+
* @var ItemFactory
33+
*/
34+
protected $itemFactory;
35+
36+
/**
37+
* @var CartHelper
38+
*/
39+
protected $cartHelper;
40+
41+
/**
42+
* @var Escaper
43+
*/
44+
protected $escaper;
45+
46+
/**
47+
* @param ActionContext $context
48+
* @param CustomerCart $cart
49+
* @param OptionFactory $optionFactory
50+
* @param ItemFactory $itemFactory
51+
* @param CartHelper $cartHelper
52+
* @param Escaper $escaper
53+
*/
54+
public function __construct(
55+
ActionContext $context,
56+
CustomerCart $cart,
57+
OptionFactory $optionFactory,
58+
ItemFactory $itemFactory,
59+
CartHelper $cartHelper,
60+
Escaper $escaper
61+
) {
62+
$this->cart = $cart;
63+
$this->optionFactory = $optionFactory;
64+
$this->itemFactory = $itemFactory;
65+
$this->cartHelper = $cartHelper;
66+
$this->escaper = $escaper;
67+
parent::__construct($context);
68+
}
69+
1270
/**
1371
* Add shared wishlist item to shopping cart
1472
*
@@ -21,29 +79,34 @@ public function execute()
2179
{
2280
$itemId = (int)$this->getRequest()->getParam('item');
2381

24-
/* @var $item \Magento\Wishlist\Model\Item */
25-
$item = $this->_objectManager->create('Magento\Wishlist\Model\Item')->load($itemId);
26-
27-
$cart = $this->_objectManager->get('Magento\Checkout\Model\Cart');
82+
/* @var $item Item */
83+
$item = $this->itemFactory->create()
84+
->load($itemId);
2885

2986
$redirectUrl = $this->_redirect->getRefererUrl();
3087

3188
try {
32-
$options = $this->_objectManager->create(
33-
'Magento\Wishlist\Model\Item\Option'
34-
)->getCollection()->addItemFilter(
35-
[$itemId]
36-
);
89+
/** @var OptionCollection $options */
90+
$options = $this->optionFactory->create()
91+
->getCollection()->addItemFilter([$itemId]);
3792
$item->setOptions($options->getOptionsByItem($itemId));
93+
$item->addToCart($this->cart);
3894

39-
$item->addToCart($cart);
40-
$cart->save()->getQuote()->collectTotals();
95+
$this->cart->save();
96+
97+
if (!$this->cart->getQuote()->getHasError()) {
98+
$message = __(
99+
'You added %1 to your shopping cart.',
100+
$this->escaper->escapeHtml($item->getProduct()->getName())
101+
);
102+
$this->messageManager->addSuccess($message);
103+
}
41104

42-
if ($this->_objectManager->get('Magento\Checkout\Helper\Cart')->getShouldRedirectToCart()) {
43-
$redirectUrl = $this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl();
105+
if ($this->cartHelper->getShouldRedirectToCart()) {
106+
$redirectUrl = $this->cartHelper->getCartUrl();
44107
}
45-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
46-
if ($e->getCode() == \Magento\Wishlist\Model\Item::EXCEPTION_CODE_NOT_SALABLE) {
108+
} catch (LocalizedException $e) {
109+
if ($e->getCode() == Item::EXCEPTION_CODE_NOT_SALABLE) {
47110
$this->messageManager->addError(__('This product(s) is out of stock.'));
48111
} else {
49112
$this->messageManager->addNotice($e->getMessage());

app/code/Magento/Wishlist/Helper/Data.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,18 @@ public function getSharedAddToCartUrl($item)
405405
);
406406
}
407407

408+
/**
409+
* Retrieve URL for adding All items to shopping cart from shared wishlist
410+
*
411+
* @return string
412+
*/
413+
public function getSharedAddAllToCartUrl()
414+
{
415+
return $this->_postDataHelper->getPostData(
416+
$this->_storeManager->getStore()->getUrl('*/*/allcart', ['_current' => true])
417+
);
418+
}
419+
408420
/**
409421
* @param string|\Magento\Catalog\Model\Product|\Magento\Wishlist\Model\Item $item
410422
* @return array

0 commit comments

Comments
 (0)