Skip to content

Commit 1f0586d

Browse files
ENGCOM-7949: Complex message added for add to cart success message #27977
- Merge Pull Request #27977 from ajithkumar-maragathavel/magento2:complex-error-message - Merged commits: 1. c2bc25a 2. 62c6f94 3. ee2f0cc 4. 812d943
2 parents 49c6846 + 812d943 commit 1f0586d

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,25 @@ private function addOrderItem(Item $item)
104104
if ($orderCustomerId == $currentCustomerId) {
105105
$this->cart->addOrderItem($item, 1);
106106
if (!$this->cart->getQuote()->getHasError()) {
107-
$message = __(
108-
'You added %1 to your shopping cart.',
109-
$this->escaper->escapeHtml($item->getName())
107+
$this->messageManager->addComplexSuccessMessage(
108+
'addCartSuccessMessage',
109+
[
110+
'product_name' => $item->getName(),
111+
'cart_url' => $this->getCartUrl()
112+
]
110113
);
111-
$this->messageManager->addSuccessMessage($message);
112114
}
113115
}
114116
}
115117
}
118+
119+
/**
120+
* Returns cart url
121+
*
122+
* @return string
123+
*/
124+
private function getCartUrl()
125+
{
126+
return $this->_url->getUrl('checkout/cart', ['_secure' => true]);
127+
}
116128
}

app/code/Magento/Wishlist/Controller/Index/Cart.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,13 @@ public function execute()
220220
$wishlist->save();
221221

222222
if (!$this->cart->getQuote()->getHasError()) {
223-
$message = __(
224-
'You added %1 to your shopping cart.',
225-
$this->escaper->escapeHtml($item->getProduct()->getName())
223+
$this->messageManager->addComplexSuccessMessage(
224+
'addCartSuccessMessage',
225+
[
226+
'product_name' => $item->getProduct()->getName(),
227+
'cart_url' => $this->cartHelper->getCartUrl()
228+
]
226229
);
227-
$this->messageManager->addSuccessMessage($message);
228-
229230
$productsToAdd = [
230231
[
231232
'sku' => $item->getProduct()->getSku(),

app/code/Magento/Wishlist/Test/Mftf/ActionGroup/StorefrontCustomerAddProductToCartFromWishlistUsingSidebarActionGroup.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
<click selector="{{StorefrontCustomerWishlistSidebarSection.ProductAddToCartByName(product.name)}}" stepKey="AddProductToCartFromWishlistUsingSidebarClickAddToCartFromWishlist"/>
2020
<waitForElement selector="{{StorefrontCategoryMainSection.SuccessMsg}}" time="30" stepKey="AddProductToCartFromWishlistUsingSidebarWaitForSuccessMessage"/>
2121
<see selector="{{StorefrontCategoryMainSection.SuccessMsg}}" userInput="You added {{product.name}} to your shopping cart." stepKey="AddProductToCartFromWishlistUsingSidebarSeeProductNameAddedToCartFromWishlist"/>
22+
<seeLink userInput="shopping cart" stepKey="seeLinkInSuccessMsg"/>
2223
</actionGroup>
2324
</actionGroups>

app/code/Magento/Wishlist/Test/Unit/Controller/Index/CartTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use Magento\Catalog\Helper\Product as ProductHelper;
1111
use Magento\Catalog\Model\Product;
1212
use Magento\Catalog\Model\Product\Exception as ProductException;
13-
use Magento\Checkout\Model\Cart as CheckoutCart;
1413
use Magento\Checkout\Helper\Cart as CartHelper;
14+
use Magento\Checkout\Model\Cart as CheckoutCart;
1515
use Magento\Framework\App\Action\Context;
1616
use Magento\Framework\App\RequestInterface;
1717
use Magento\Framework\App\Response\RedirectInterface;
@@ -620,14 +620,8 @@ protected function prepareExecuteWithQuantityArray($isAjax = false)
620620
->method('getName')
621621
->willReturn($productName);
622622

623-
$this->escaperMock->expects($this->once())
624-
->method('escapeHtml')
625-
->with($productName, null)
626-
->willReturn($productName);
627-
628623
$this->messageManagerMock->expects($this->once())
629-
->method('addSuccessMessage')
630-
->with('You added ' . $productName . ' to your shopping cart.', null)
624+
->method('addComplexSuccessMessage')
631625
->willReturnSelf();
632626

633627
$this->cartHelperMock->expects($this->once())

dev/tests/integration/testsuite/Magento/Wishlist/Controller/Index/CartTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public function testAddSimpleProductToCart(): void
6969
$item = $this->getWishlistByCustomerId->getItemBySku(1, 'simple-1');
7070
$this->assertNotNull($item);
7171
$this->performAddToCartRequest(['item' => $item->getId(), 'qty' => 3]);
72-
$message = sprintf('You added %s to your shopping cart.', $item->getName());
72+
$message = sprintf("\n" . 'You added %s to your ' .
73+
'<a href="http://localhost/index.php/checkout/cart/">shopping cart</a>.', $item->getName());
7374
$this->assertSessionMessages($this->equalTo([(string)__($message)]), MessageInterface::TYPE_SUCCESS);
7475
$this->assertCount(0, $this->getWishlistByCustomerId->execute(1)->getItemCollection());
7576
$cart = $this->cartFactory->create();

0 commit comments

Comments
 (0)