Skip to content

Commit 3232465

Browse files
committed
Add a link to the cart to the success message when adding a product
1 parent ec47891 commit 3232465

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,14 @@ 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()
125+
$this->messageManager->addComplexSuccessMessage(
126+
'addCartSuccessMessage',
127+
[
128+
'product_name' => $product->getName(),
129+
'cart_url' => $this->getCartUrl(),
130+
]
128131
);
129-
$this->messageManager->addSuccessMessage($message);
132+
130133
}
131134
return $this->goBack(null, $product);
132135
}
@@ -147,8 +150,7 @@ public function execute()
147150
$url = $this->_checkoutSession->getRedirectUrl(true);
148151

149152
if (!$url) {
150-
$cartUrl = $this->_objectManager->get(\Magento\Checkout\Helper\Cart::class)->getCartUrl();
151-
$url = $this->_redirect->getRedirectUrl($cartUrl);
153+
$url = $this->_redirect->getRedirectUrl($this->getCartUrl());
152154
}
153155

154156
return $this->goBack($url);
@@ -188,4 +190,12 @@ protected function goBack($backUrl = null, $product = null)
188190
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
189191
);
190192
}
193+
194+
/**
195+
* @return string
196+
*/
197+
private function getCartUrl()
198+
{
199+
return $this->_url->getUrl('checkout/cart', ['_secure' => true]);
200+
}
191201
}

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']);

0 commit comments

Comments
 (0)