Skip to content

Commit

Permalink
ENGCOM-1534: Add a link to the cart to the success message when addin…
Browse files Browse the repository at this point in the history
…g a product (Magento 2.3) #14059
  • Loading branch information
Stanislav Idolov authored Sep 14, 2018
2 parents deb49a7 + d4a33ce commit 330a33f
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 21 deletions.
18 changes: 12 additions & 6 deletions app/code/Magento/Checkout/Controller/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,7 @@ protected function getBackUrl($defaultUrl = null)
return $returnUrl;
}

$shouldRedirectToCart = $this->_scopeConfig->getValue(
'checkout/cart/redirect_to_cart',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);

if ($shouldRedirectToCart || $this->getRequest()->getParam('in_cart')) {
if ($this->shouldRedirectToCart() || $this->getRequest()->getParam('in_cart')) {
if ($this->getRequest()->getActionName() == 'add' && !$this->getRequest()->getParam('in_cart')) {
$this->_checkoutSession->setContinueShoppingUrl($this->_redirect->getRefererUrl());
}
Expand All @@ -132,4 +127,15 @@ protected function getBackUrl($defaultUrl = null)

return $defaultUrl;
}

/**
* @return bool
*/
private function shouldRedirectToCart()
{
return $this->_scopeConfig->isSetFlag(
'checkout/cart/redirect_to_cart',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
42 changes: 35 additions & 7 deletions app/code/Magento/Checkout/Controller/Cart/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,21 @@ public function execute()

if (!$this->_checkoutSession->getNoCartRedirect(true)) {
if (!$this->cart->getQuote()->getHasError()) {
$message = __(
'You added %1 to your shopping cart.',
$product->getName()
);
$this->messageManager->addSuccessMessage($message);
if ($this->shouldRedirectToCart()) {
$message = __(
'You added %1 to your shopping cart.',
$product->getName()
);
$this->messageManager->addSuccessMessage($message);
} else {
$this->messageManager->addComplexSuccessMessage(
'addCartSuccessMessage',
[
'product_name' => $product->getName(),
'cart_url' => $this->getCartUrl(),
]
);
}
}
return $this->goBack(null, $product);
}
Expand All @@ -150,8 +160,7 @@ public function execute()
$url = $this->_checkoutSession->getRedirectUrl(true);

if (!$url) {
$cartUrl = $this->_objectManager->get(\Magento\Checkout\Helper\Cart::class)->getCartUrl();
$url = $this->_redirect->getRedirectUrl($cartUrl);
$url = $this->_redirect->getRedirectUrl($this->getCartUrl());
}

return $this->goBack($url);
Expand Down Expand Up @@ -194,4 +203,23 @@ protected function goBack($backUrl = null, $product = null)
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
);
}

/**
* @return string
*/
private function getCartUrl()
{
return $this->_url->getUrl('checkout/cart', ['_secure' => true]);
}

/**
* @return bool
*/
private function shouldRedirectToCart()
{
return $this->_scopeConfig->isSetFlag(
'checkout/cart/redirect_to_cart',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
12 changes: 12 additions & 0 deletions app/code/Magento/Checkout/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
</argument>
</arguments>
</type>
<type name="Magento\Framework\View\Element\Message\MessageConfigurationsPool">
<arguments>
<argument name="configurationsMap" xsi:type="array">
<item name="addCartSuccessMessage" xsi:type="array">
<item name="renderer" xsi:type="const">\Magento\Framework\View\Element\Message\Renderer\BlockRenderer::CODE</item>
<item name="data" xsi:type="array">
<item name="template" xsi:type="string">Magento_Checkout::messages/addCartSuccessMessage.phtml</item>
</item>
</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/** @var \Magento\Framework\View\Element\Template $block */
?>

<?= $block->escapeHtml(__(
'You added %1 to your <a href="%2">shopping cart</a>.',
$block->getData('product_name'),
$block->getData('cart_url')
), ['a']);
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,75 @@ public function addAddProductDataProvider()
];
}

/**
* Test for \Magento\Checkout\Controller\Cart\Add::execute() with simple product and activated redirect to cart
*
* @magentoDataFixture Magento/Catalog/_files/products.php
* @magentoConfigFixture current_store checkout/cart/redirect_to_cart 1
* @magentoAppIsolation enabled
*/
public function testMessageAtAddToCartWithRedirect()
{
$formKey = $this->_objectManager->get(FormKey::class);
$postData = [
'qty' => '1',
'product' => '1',
'custom_price' => 1,
'form_key' => $formKey->getFormKey(),
'isAjax' => 1
];
\Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea('frontend');
$this->getRequest()->setPostValue($postData);

$this->dispatch('checkout/cart/add');

$this->assertEquals(
'{"backUrl":"http:\/\/localhost\/index.php\/checkout\/cart\/"}',
$this->getResponse()->getBody()
);

$this->assertSessionMessages(
$this->contains(
'You added Simple Product to your shopping cart.'
),
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
);
}

/**
* Test for \Magento\Checkout\Controller\Cart\Add::execute() with simple product and deactivated redirect to cart
*
* @magentoDataFixture Magento/Catalog/_files/products.php
* @magentoConfigFixture current_store checkout/cart/redirect_to_cart 0
* @magentoAppIsolation enabled
*/
public function testMessageAtAddToCartWithoutRedirect()
{
$formKey = $this->_objectManager->get(FormKey::class);
$postData = [
'qty' => '1',
'product' => '1',
'custom_price' => 1,
'form_key' => $formKey->getFormKey(),
'isAjax' => 1
];
\Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea('frontend');
$this->getRequest()->setPostValue($postData);

$this->dispatch('checkout/cart/add');

$this->assertFalse($this->getResponse()->isRedirect());
$this->assertEquals('[]', $this->getResponse()->getBody());

$this->assertSessionMessages(
$this->contains(
"\n" . 'You added Simple Product to your ' .
'<a href="http://localhost/index.php/checkout/cart/">shopping cart</a>.'
),
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
);
}

/**
* @covers \Magento\Checkout\Controller\Cart\Addgroup::execute()
*
Expand Down
16 changes: 8 additions & 8 deletions setup/performance-toolkit/benchmark.jmx
Original file line number Diff line number Diff line change
Expand Up @@ -4940,7 +4940,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
Expand Down Expand Up @@ -5306,7 +5306,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
Expand Down Expand Up @@ -6821,7 +6821,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
Expand Down Expand Up @@ -7187,7 +7187,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
Expand Down Expand Up @@ -8139,7 +8139,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
Expand Down Expand Up @@ -8505,7 +8505,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
Expand Down Expand Up @@ -9963,7 +9963,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
Expand Down Expand Up @@ -10329,7 +10329,7 @@ vars.put("totalProductsAdded", String.valueOf(productsAdded));
<hashTree>
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Response Assertion" enabled="true">
<collectionProp name="Asserion.test_strings">
<stringProp name="210217247">You added ${product_name} to your shopping cart.</stringProp>
<stringProp name="210217247">You added ${product_name} to your <a href="${base_path}checkout/cart/">shopping cart</a>.</stringProp>
</collectionProp>
<stringProp name="Assertion.test_field">Assertion.response_data</stringProp>
<boolProp name="Assertion.assume_success">false</boolProp>
Expand Down

0 comments on commit 330a33f

Please sign in to comment.