Skip to content

Commit f9b2c68

Browse files
committed
Merge branch 'MC-42813' of https://github.com/magento-l3/magento2ce into PR-2021-20-08
2 parents 1483c7a + 7acf1bc commit f9b2c68

File tree

5 files changed

+125
-31
lines changed

5 files changed

+125
-31
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
/**
2323
* UpdateItemQty ajax request
24-
*
25-
* @package Magento\Checkout\Controller\Cart
2624
*/
2725
class UpdateItemQty extends Action implements HttpPostActionInterface
2826
{
@@ -97,17 +95,23 @@ public function execute()
9795
$cartData = $this->quantityProcessor->process($cartData);
9896
$quote = $this->checkoutSession->getQuote();
9997

98+
$response = [];
10099
foreach ($cartData as $itemId => $itemInfo) {
101100
$item = $quote->getItemById($itemId);
102101
$qty = isset($itemInfo['qty']) ? (double) $itemInfo['qty'] : 0;
103102
if ($item) {
104-
$this->updateItemQuantity($item, $qty);
103+
try {
104+
$this->updateItemQuantity($item, $qty);
105+
} catch (LocalizedException $e) {
106+
$response[] = [
107+
'error' => $e->getMessage(),
108+
'itemId' => $itemId
109+
];
110+
}
105111
}
106112
}
107113

108-
$this->jsonResponse();
109-
} catch (LocalizedException $e) {
110-
$this->jsonResponse($e->getMessage());
114+
$this->jsonResponse(count($response)? json_encode($response) : '');
111115
} catch (\Exception $e) {
112116
$this->logger->critical($e->getMessage());
113117
$this->jsonResponse('Something went wrong while saving the page. Please refresh the page and try again.');
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="UpdateProductFromShoppingCartTest">
12+
<annotations>
13+
<stories value="Shopping Cart"/>
14+
<title value="Cart qty update should be reverted back if the requested qty is not available"/>
15+
<description value="Update Product Qty on Shopping Cart when the requested qty is not available"/>
16+
<severity value="MAJOR"/>
17+
<testCaseId value="MC-42907"/>
18+
<group value="shoppingCart"/>
19+
<group value="mtf_migrated"/>
20+
</annotations>
21+
22+
<before>
23+
<!--Create product according to dataset.-->
24+
<createData entity="simpleProductWithoutCategory" stepKey="createProduct"/>
25+
26+
<!--Add product to cart-->
27+
<actionGroup ref="AddSimpleProductToCartActionGroup" stepKey="addToCartFromStorefrontProductPage">
28+
<argument name="product" value="$$createProduct$$"/>
29+
</actionGroup>
30+
</before>
31+
32+
<after>
33+
<!--Delete created data-->
34+
<deleteData createDataKey="createProduct" stepKey="deleteProduct" />
35+
</after>
36+
37+
<!--Go to cart page, update qty -->
38+
<actionGroup ref="StorefrontCartPageOpenActionGroup" stepKey="goToCartPage"/>
39+
<see userInput="Shopping Cart" stepKey="seeCartPageIsOpened"/>
40+
41+
<fillField selector="{{CheckoutCartProductSection.qty($$createProduct.sku$$)}}" userInput="1001" stepKey="updateProductQty"/>
42+
<click selector="{{CheckoutCartProductSection.updateShoppingCartButton}}" stepKey="clickUpdateShoppingCart"/>
43+
<waitForAjaxLoad stepKey="waitForAjaxLoad"/>
44+
<click selector=".modal-footer button[data-role='action']" stepKey="clickAlertMessage"/>
45+
<grabValueFrom selector="{{CheckoutCartProductSection.qty($$createProduct.sku$$)}}" stepKey="grabQty"/>
46+
<assertEquals stepKey="assertQty">
47+
<actualResult type="const">$grabQty</actualResult>
48+
<expectedResult type="const">1</expectedResult>
49+
</assertEquals>
50+
</test>
51+
</tests>

app/code/Magento/Checkout/view/frontend/templates/cart/item/default.phtml

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
// phpcs:disable Magento2.Templates.ThisInTemplate
8-
// phpcs:disable Magento2.Files.LineLength.MaxExceeded
8+
// phpcs:disable Generic.Files.LineLength
99

1010
/** @var $block \Magento\Checkout\Block\Cart\Item\Renderer */
1111

@@ -19,58 +19,58 @@ $canApplyMsrp = $helper->isShowBeforeOrderConfirm($product) && $helper->isMinima
1919
<tbody class="cart item">
2020
<tr class="item-info">
2121
<td data-th="<?= $block->escapeHtml(__('Item')) ?>" class="col item">
22-
<?php if ($block->hasProductUrl()) :?>
22+
<?php if ($block->hasProductUrl()): ?>
2323
<a href="<?= $block->escapeUrl($block->getProductUrl()) ?>"
2424
title="<?= $block->escapeHtml($block->getProductName()) ?>"
2525
tabindex="-1"
2626
class="product-item-photo">
27-
<?php else :?>
27+
<?php else: ?>
2828
<span class="product-item-photo">
29-
<?php endif;?>
29+
<?php endif; ?>
3030
<?= $block->getImage($block->getProductForThumbnail(), 'cart_page_product_thumbnail')->toHtml() ?>
31-
<?php if ($block->hasProductUrl()) :?>
31+
<?php if ($block->hasProductUrl()): ?>
3232
</a>
33-
<?php else :?>
33+
<?php else: ?>
3434
</span>
3535
<?php endif; ?>
3636
<div class="product-item-details">
3737
<strong class="product-item-name">
38-
<?php if ($block->hasProductUrl()) :?>
38+
<?php if ($block->hasProductUrl()): ?>
3939
<a href="<?= $block->escapeUrl($block->getProductUrl()) ?>"><?= $block->escapeHtml($block->getProductName()) ?></a>
40-
<?php else :?>
40+
<?php else: ?>
4141
<?= $block->escapeHtml($block->getProductName()) ?>
4242
<?php endif; ?>
4343
</strong>
44-
<?php if ($_options = $block->getOptionList()) :?>
44+
<?php if ($_options = $block->getOptionList()): ?>
4545
<dl class="item-options">
46-
<?php foreach ($_options as $_option) :?>
46+
<?php foreach ($_options as $_option): ?>
4747
<?php $_formatedOptionValue = $block->getFormatedOptionValue($_option) ?>
4848
<dt><?= $block->escapeHtml($_option['label']) ?></dt>
4949
<dd>
50-
<?php if (isset($_formatedOptionValue['full_view'])) :?>
50+
<?php if (isset($_formatedOptionValue['full_view'])): ?>
5151
<?= $block->escapeHtml($_formatedOptionValue['full_view'], ['span', 'a']) ?>
52-
<?php else :?>
53-
<?= $block->escapeHtml($_formatedOptionValue['value'], ['span', 'a']) ?>
52+
<?php else: ?>
53+
<?= $block->escapeHtml($_formatedOptionValue['value'], ['span', 'a']) ?>
5454
<?php endif; ?>
5555
</dd>
5656
<?php endforeach; ?>
5757
</dl>
58-
<?php endif;?>
59-
<?php if ($messages = $block->getMessages()) :?>
60-
<?php foreach ($messages as $message) :?>
58+
<?php endif; ?>
59+
<?php if ($messages = $block->getMessages()): ?>
60+
<?php foreach ($messages as $message): ?>
6161
<div class= "cart item message <?= $block->escapeHtmlAttr($message['type']) ?>">
6262
<div><?= $block->escapeHtml($message['text']) ?></div>
6363
</div>
6464
<?php endforeach; ?>
6565
<?php endif; ?>
6666
<?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?>
67-
<?php if ($addInfoBlock) :?>
67+
<?php if ($addInfoBlock): ?>
6868
<?= $addInfoBlock->setItem($_item)->toHtml() ?>
69-
<?php endif;?>
69+
<?php endif; ?>
7070
</div>
7171
</td>
7272

73-
<?php if ($canApplyMsrp) :?>
73+
<?php if ($canApplyMsrp): ?>
7474
<td class="col msrp" data-th="<?= $block->escapeHtml(__('Price')) ?>">
7575
<span class="pricing msrp">
7676
<span class="msrp notice"><?= $block->escapeHtml(__('See price before order confirmation.')) ?></span>
@@ -89,7 +89,7 @@ $canApplyMsrp = $helper->isShowBeforeOrderConfirm($product) && $helper->isMinima
8989
</a>
9090
</span>
9191
</td>
92-
<?php else :?>
92+
<?php else: ?>
9393
<td class="col price" data-th="<?= $block->escapeHtml(__('Price')) ?>">
9494
<?= $block->getUnitPriceHtml($_item) ?>
9595
</td>
@@ -109,16 +109,17 @@ $canApplyMsrp = $helper->isShowBeforeOrderConfirm($product) && $helper->isMinima
109109
title="<?= $block->escapeHtmlAttr(__('Qty')) ?>"
110110
class="input-text qty"
111111
data-validate="{required:true,'validate-greater-than-zero':true}"
112+
data-item-qty="<?= $block->escapeHtmlAttr($block->getQty()) ?>"
112113
data-role="cart-item-qty"/>
113114
</label>
114115
</div>
115116
</div>
116117
</td>
117118

118119
<td class="col subtotal" data-th="<?= $block->escapeHtml(__('Subtotal')) ?>">
119-
<?php if ($canApplyMsrp) :?>
120+
<?php if ($canApplyMsrp): ?>
120121
<span class="cart msrp subtotal">--</span>
121-
<?php else :?>
122+
<?php else: ?>
122123
<?= $block->getRowTotalHtml($_item) ?>
123124
<?php endif; ?>
124125
</td>

app/code/Magento/Checkout/view/frontend/web/js/action/update-shopping-cart.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,29 @@ define([
107107
* Form validation failed.
108108
*/
109109
onError: function (response) {
110+
var that = this,
111+
elm,
112+
responseData = JSON.parse(response['error_message']);
113+
110114
if (response['error_message']) {
115+
try {
116+
$.each(responseData, function (index, data) {
117+
118+
if (data.itemId !== undefined) {
119+
elm = $('#cart-' + data.itemId + '-qty');
120+
elm.val(elm.attr('data-item-qty'));
121+
}
122+
response['error_message'] = data.error;
123+
});
124+
} catch (e) {}
111125
alert({
112-
content: response['error_message']
126+
content: response['error_message'],
127+
actions: {
128+
/** @inheritdoc */
129+
always: function () {
130+
that.submitForm();
131+
}
132+
}
113133
});
114134
} else {
115135
this.submitForm();

dev/tests/integration/testsuite/Magento/Checkout/Controller/Cart/UpdateItemQtyTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,25 @@ public function testExecute($requestQuantity, $expectedResponse)
8787
$this->dispatch('checkout/cart/updateItemQty');
8888
$response = $this->getResponse()->getBody();
8989

90-
$this->assertEquals($this->json->unserialize($response), $expectedResponse);
90+
$this->assertEquals($this->getErrorMessage($response), $this->getErrorMessage($expectedResponse));
91+
}
92+
93+
/**
94+
* @param $response
95+
* @return string
96+
*/
97+
protected function getErrorMessage($response)
98+
{
99+
$error = '';
100+
try {
101+
$data = is_array($response) ? $response : $this->json->unserialize($response);
102+
$error = $this->json->unserialize($data['error_message'])[0]['error'];
103+
} catch (\Exception $e) {
104+
if (!empty($data['error_message'])) {
105+
$error = $data['error_message'];
106+
}
107+
}
108+
return $error;
91109
}
92110

93111
/**
@@ -115,7 +133,7 @@ public function requestDataProvider(): array
115133
'request' => ['qty' => 230],
116134
'response' => [
117135
'success' => false,
118-
'error_message' => 'The requested qty is not available']
136+
'error_message' => '[{"error":"The requested qty is not available","itemId":3}]']
119137
],
120138
];
121139
}

0 commit comments

Comments
 (0)