Skip to content

Commit dd21a70

Browse files
ENGCOM-1534: Add a link to the cart to the success message when adding a product (Magento 2.3) #14059
- Merge Pull Request #14059 from avstudnitz/magento2:add-cart-success-message-2-3 - Merged commits: 1. 4a4a2cd 2. f5f40fa 3. 49bee81 4. c67a76f 5. 97c6b0a 6. b93ed4d
2 parents 166dd61 + b93ed4d commit dd21a70

File tree

5 files changed

+142
-13
lines changed

5 files changed

+142
-13
lines changed

app/code/Magento/Checkout/Controller/Cart.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,7 @@ protected function getBackUrl($defaultUrl = null)
118118
return $returnUrl;
119119
}
120120

121-
$shouldRedirectToCart = $this->_scopeConfig->getValue(
122-
'checkout/cart/redirect_to_cart',
123-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
124-
);
125-
126-
if ($shouldRedirectToCart || $this->getRequest()->getParam('in_cart')) {
121+
if ($this->shouldRedirectToCart() || $this->getRequest()->getParam('in_cart')) {
127122
if ($this->getRequest()->getActionName() == 'add' && !$this->getRequest()->getParam('in_cart')) {
128123
$this->_checkoutSession->setContinueShoppingUrl($this->_redirect->getRefererUrl());
129124
}
@@ -132,4 +127,15 @@ protected function getBackUrl($defaultUrl = null)
132127

133128
return $defaultUrl;
134129
}
130+
131+
/**
132+
* @return bool
133+
*/
134+
private function shouldRedirectToCart()
135+
{
136+
return $this->_scopeConfig->isSetFlag(
137+
'checkout/cart/redirect_to_cart',
138+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
139+
);
140+
}
135141
}

app/code/Magento/Checkout/Controller/Cart/Add.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,21 @@ public function execute()
122122

123123
if (!$this->_checkoutSession->getNoCartRedirect(true)) {
124124
if (!$this->cart->getQuote()->getHasError()) {
125-
$message = __(
126-
'You added %1 to your shopping cart.',
127-
$product->getName()
128-
);
129-
$this->messageManager->addSuccessMessage($message);
125+
if ($this->shouldRedirectToCart()) {
126+
$message = __(
127+
'You added %1 to your shopping cart.',
128+
$product->getName()
129+
);
130+
$this->messageManager->addSuccessMessage($message);
131+
} else {
132+
$this->messageManager->addComplexSuccessMessage(
133+
'addCartSuccessMessage',
134+
[
135+
'product_name' => $product->getName(),
136+
'cart_url' => $this->getCartUrl(),
137+
]
138+
);
139+
}
130140
}
131141
return $this->goBack(null, $product);
132142
}
@@ -147,8 +157,7 @@ public function execute()
147157
$url = $this->_checkoutSession->getRedirectUrl(true);
148158

149159
if (!$url) {
150-
$cartUrl = $this->_objectManager->get(\Magento\Checkout\Helper\Cart::class)->getCartUrl();
151-
$url = $this->_redirect->getRedirectUrl($cartUrl);
160+
$url = $this->_redirect->getRedirectUrl($this->getCartUrl());
152161
}
153162

154163
return $this->goBack($url);
@@ -191,4 +200,23 @@ protected function goBack($backUrl = null, $product = null)
191200
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
192201
);
193202
}
203+
204+
/**
205+
* @return string
206+
*/
207+
private function getCartUrl()
208+
{
209+
return $this->_url->getUrl('checkout/cart', ['_secure' => true]);
210+
}
211+
212+
/**
213+
* @return bool
214+
*/
215+
private function shouldRedirectToCart()
216+
{
217+
return $this->_scopeConfig->isSetFlag(
218+
'checkout/cart/redirect_to_cart',
219+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
220+
);
221+
}
194222
}

app/code/Magento/Checkout/etc/frontend/di.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,16 @@
8383
</argument>
8484
</arguments>
8585
</type>
86+
<type name="Magento\Framework\View\Element\Message\MessageConfigurationsPool">
87+
<arguments>
88+
<argument name="configurationsMap" xsi:type="array">
89+
<item name="addCartSuccessMessage" xsi:type="array">
90+
<item name="renderer" xsi:type="const">\Magento\Framework\View\Element\Message\Renderer\BlockRenderer::CODE</item>
91+
<item name="data" xsi:type="array">
92+
<item name="template" xsi:type="string">Magento_Checkout::messages/addCartSuccessMessage.phtml</item>
93+
</item>
94+
</item>
95+
</argument>
96+
</arguments>
97+
</type>
8698
</config>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
// @codingStandardsIgnoreFile
7+
/** @var \Magento\Framework\View\Element\Template $block */
8+
?>
9+
10+
<?= $block->escapeHtml(__(
11+
'You added %1 to your <a href="%2">shopping cart</a>.',
12+
$block->getData('product_name'),
13+
$block->getData('cart_url')
14+
), ['a']);

dev/tests/integration/testsuite/Magento/Checkout/Controller/CartTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,75 @@ public function addAddProductDataProvider()
300300
];
301301
}
302302

303+
/**
304+
* Test for \Magento\Checkout\Controller\Cart\Add::execute() with simple product and activated redirect to cart
305+
*
306+
* @magentoDataFixture Magento/Catalog/_files/products.php
307+
* @magentoConfigFixture current_store checkout/cart/redirect_to_cart 1
308+
* @magentoAppIsolation enabled
309+
*/
310+
public function testMessageAtAddToCartWithRedirect()
311+
{
312+
$formKey = $this->_objectManager->get(FormKey::class);
313+
$postData = [
314+
'qty' => '1',
315+
'product' => '1',
316+
'custom_price' => 1,
317+
'form_key' => $formKey->getFormKey(),
318+
'isAjax' => 1
319+
];
320+
\Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea('frontend');
321+
$this->getRequest()->setPostValue($postData);
322+
323+
$this->dispatch('checkout/cart/add');
324+
325+
$this->assertEquals(
326+
'{"backUrl":"http:\/\/localhost\/index.php\/checkout\/cart\/"}',
327+
$this->getResponse()->getBody()
328+
);
329+
330+
$this->assertSessionMessages(
331+
$this->contains(
332+
'You added Simple Product to your shopping cart.'
333+
),
334+
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
335+
);
336+
}
337+
338+
/**
339+
* Test for \Magento\Checkout\Controller\Cart\Add::execute() with simple product and deactivated redirect to cart
340+
*
341+
* @magentoDataFixture Magento/Catalog/_files/products.php
342+
* @magentoConfigFixture current_store checkout/cart/redirect_to_cart 0
343+
* @magentoAppIsolation enabled
344+
*/
345+
public function testMessageAtAddToCartWithoutRedirect()
346+
{
347+
$formKey = $this->_objectManager->get(FormKey::class);
348+
$postData = [
349+
'qty' => '1',
350+
'product' => '1',
351+
'custom_price' => 1,
352+
'form_key' => $formKey->getFormKey(),
353+
'isAjax' => 1
354+
];
355+
\Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea('frontend');
356+
$this->getRequest()->setPostValue($postData);
357+
358+
$this->dispatch('checkout/cart/add');
359+
360+
$this->assertFalse($this->getResponse()->isRedirect());
361+
$this->assertEquals('[]', $this->getResponse()->getBody());
362+
363+
$this->assertSessionMessages(
364+
$this->contains(
365+
"\n" . 'You added Simple Product to your ' .
366+
'<a href="http://localhost/index.php/checkout/cart/">shopping cart</a>.'
367+
),
368+
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
369+
);
370+
}
371+
303372
/**
304373
* @covers \Magento\Checkout\Controller\Cart\Addgroup::execute()
305374
*

0 commit comments

Comments
 (0)