Skip to content

Commit ee59325

Browse files
🔃 [EngCom] Public Pull Requests - 2.2-develop
Accepted Public Pull Requests: - #14428: Fix \Magento\Checkout\Controller\Index\Index::isSecureRequest method to take care of current request being secure and also from referer, as stated in phpdoc block (by @adrian-martinez-interactiv4) - #13904: Add a link to the cart to the success message when adding a product (by @avstudnitz) Fixed GitHub Issues: - #4301: Hit fast twice F5 on checout page, customer loggs out automatically (reported by @daniel-ifrim) has been fixed in #14428 by @adrian-martinez-interactiv4 in 2.2-develop branch Related commits: 1. dc687c6 - #12362: Concurrent (quick reload) requests on checkout cause cart to empty - related to session_regenerate_id (reported by @minlare) has been fixed in #14428 by @adrian-martinez-interactiv4 in 2.2-develop branch Related commits: 1. dc687c6 - #13427: [2.1.11] Add to cart, try to checkout, cart is empty but mini-cart has items. (reported by @aeu) has been fixed in #14428 by @adrian-martinez-interactiv4 in 2.2-develop branch Related commits: 1. dc687c6
2 parents de83f82 + 2751cdb commit ee59325

File tree

8 files changed

+165
-37
lines changed

8 files changed

+165
-37
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);
@@ -188,4 +197,23 @@ protected function goBack($backUrl = null, $product = null)
188197
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
189198
);
190199
}
200+
201+
/**
202+
* @return string
203+
*/
204+
private function getCartUrl()
205+
{
206+
return $this->_url->getUrl('checkout/cart', ['_secure' => true]);
207+
}
208+
209+
/**
210+
* @return bool
211+
*/
212+
private function shouldRedirectToCart()
213+
{
214+
return $this->_scopeConfig->isSetFlag(
215+
'checkout/cart/redirect_to_cart',
216+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
217+
);
218+
}
191219
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,16 @@ public function execute()
5151
*/
5252
private function isSecureRequest(): bool
5353
{
54-
$secure = false;
5554
$request = $this->getRequest();
5655

57-
if ($request->isSecure()) {
58-
$secure = true;
59-
}
56+
$referrer = $request->getHeader('referer');
57+
$secure = false;
6058

61-
if ($request->getHeader('referer')) {
62-
$scheme = parse_url($request->getHeader('referer'), PHP_URL_SCHEME);
59+
if ($referrer) {
60+
$scheme = parse_url($referrer, PHP_URL_SCHEME);
6361
$secure = $scheme === 'https';
6462
}
6563

66-
return $secure;
64+
return $secure && $request->isSecure();
6765
}
6866
}

app/code/Magento/Checkout/Test/Unit/Controller/Index/IndexTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,26 +236,27 @@ public function testRegenerateSessionIdOnExecute(bool $secure, string $referer,
236236
public function sessionRegenerationDataProvider(): array
237237
{
238238
return [
239+
[
240+
'secure' => false,
241+
'referer' => 'https://test.domain.com/',
242+
'expectedCall' => self::once()
243+
],
239244
[
240245
'secure' => true,
241246
'referer' => false,
242-
'expectedCall' => self::never()
247+
'expectedCall' => self::once()
243248
],
244249
[
245250
'secure' => true,
246-
'referer' => 'https://test.domain.com/',
247-
'expectedCall' => self::never()
251+
'referer' => 'http://test.domain.com/',
252+
'expectedCall' => self::once()
248253
],
254+
// This is the only case in which session regeneration can be skipped
249255
[
250-
'secure' => false,
256+
'secure' => true,
251257
'referer' => 'https://test.domain.com/',
252258
'expectedCall' => self::never()
253259
],
254-
[
255-
'secure' => true,
256-
'referer' => 'http://test.domain.com/',
257-
'expectedCall' => self::once()
258-
]
259260
];
260261
}
261262

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
*

setup/performance-toolkit/benchmark.jmx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4182,7 +4182,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
41824182
<hashTree>
41834183
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
41844184
<collectionProp name="Asserion.test_strings">
4185-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
4185+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
41864186
</collectionProp>
41874187
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
41884188
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -4548,7 +4548,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
45484548
<hashTree>
45494549
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
45504550
<collectionProp name="Asserion.test_strings">
4551-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
4551+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
45524552
</collectionProp>
45534553
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
45544554
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -6063,7 +6063,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
60636063
<hashTree>
60646064
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
60656065
<collectionProp name="Asserion.test_strings">
6066-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
6066+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
60676067
</collectionProp>
60686068
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
60696069
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -6429,7 +6429,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
64296429
<hashTree>
64306430
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
64316431
<collectionProp name="Asserion.test_strings">
6432-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
6432+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
64336433
</collectionProp>
64346434
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
64356435
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -7369,7 +7369,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
73697369
<hashTree>
73707370
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
73717371
<collectionProp name="Asserion.test_strings">
7372-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
7372+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
73737373
</collectionProp>
73747374
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
73757375
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -7735,7 +7735,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
77357735
<hashTree>
77367736
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
77377737
<collectionProp name="Asserion.test_strings">
7738-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
7738+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
77397739
</collectionProp>
77407740
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
77417741
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -9181,7 +9181,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
91819181
<hashTree>
91829182
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
91839183
<collectionProp name="Asserion.test_strings">
9184-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
9184+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
91859185
</collectionProp>
91869186
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
91879187
<boolProp name="Assertion.assume_success">false</boolProp>
@@ -9547,7 +9547,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
95479547
<hashTree>
95489548
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
95499549
<collectionProp name="Asserion.test_strings">
9550-
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
9550+
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
95519551
</collectionProp>
95529552
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
95539553
<boolProp name="Assertion.assume_success">false</boolProp>

0 commit comments

Comments
 (0)