Skip to content

Commit 330a33f

Browse files
author
Stanislav Idolov
authored
ENGCOM-1534: Add a link to the cart to the success message when adding a product (Magento 2.3) #14059
2 parents deb49a7 + d4a33ce commit 330a33f

File tree

6 files changed

+150
-21
lines changed

6 files changed

+150
-21
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
@@ -125,11 +125,21 @@ public function execute()
125125

126126
if (!$this->_checkoutSession->getNoCartRedirect(true)) {
127127
if (!$this->cart->getQuote()->getHasError()) {
128-
$message = __(
129-
'You added %1 to your shopping cart.',
130-
$product->getName()
131-
);
132-
$this->messageManager->addSuccessMessage($message);
128+
if ($this->shouldRedirectToCart()) {
129+
$message = __(
130+
'You added %1 to your shopping cart.',
131+
$product->getName()
132+
);
133+
$this->messageManager->addSuccessMessage($message);
134+
} else {
135+
$this->messageManager->addComplexSuccessMessage(
136+
'addCartSuccessMessage',
137+
[
138+
'product_name' => $product->getName(),
139+
'cart_url' => $this->getCartUrl(),
140+
]
141+
);
142+
}
133143
}
134144
return $this->goBack(null, $product);
135145
}
@@ -150,8 +160,7 @@ public function execute()
150160
$url = $this->_checkoutSession->getRedirectUrl(true);
151161

152162
if (!$url) {
153-
$cartUrl = $this->_objectManager->get(\Magento\Checkout\Helper\Cart::class)->getCartUrl();
154-
$url = $this->_redirect->getRedirectUrl($cartUrl);
163+
$url = $this->_redirect->getRedirectUrl($this->getCartUrl());
155164
}
156165

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

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
@@ -301,6 +301,75 @@ public function addAddProductDataProvider()
301301
];
302302
}
303303

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

setup/performance-toolkit/benchmark.jmx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4940,7 +4940,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
49404940
<hashTree>
49414941
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
49424942
<collectionProp name="Asserion.test_strings">
4943-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
4943+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
49444944
</collectionProp>
49454945
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
49464946
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -5306,7 +5306,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
53065306
<hashTree>
53075307
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
53085308
<collectionProp name="Asserion.test_strings">
5309-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
5309+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
53105310
</collectionProp>
53115311
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
53125312
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -6821,7 +6821,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
68216821
<hashTree>
68226822
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
68236823
<collectionProp name="Asserion.test_strings">
6824-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
6824+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
68256825
</collectionProp>
68266826
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
68276827
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -7187,7 +7187,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
71877187
<hashTree>
71887188
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
71897189
<collectionProp name="Asserion.test_strings">
7190-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
7190+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
71917191
</collectionProp>
71927192
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
71937193
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -8139,7 +8139,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
81398139
<hashTree>
81408140
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
81418141
<collectionProp name="Asserion.test_strings">
8142-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
8142+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
81438143
</collectionProp>
81448144
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
81458145
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -8505,7 +8505,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
85058505
<hashTree>
85068506
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
85078507
<collectionProp name="Asserion.test_strings">
8508-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
8508+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
85098509
</collectionProp>
85108510
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
85118511
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -9963,7 +9963,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
99639963
<hashTree>
99649964
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
99659965
<collectionProp name="Asserion.test_strings">
9966-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
9966+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
99679967
</collectionProp>
99689968
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
99699969
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -10329,7 +10329,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
1032910329
<hashTree>
1033010330
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
1033110331
<collectionProp name="Asserion.test_strings">
10332-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
10332+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
1033310333
</collectionProp>
1033410334
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
1033510335
<boolProp name="Assertion.assume_success">false</boolProp>

0 commit comments

Comments
 (0)