Skip to content

Commit 4e73ff3

Browse files
author
Anna Bukatar
committed
MC-42813: Feature request: Cart qty update should be reverted back if the requested qty is not available
1 parent 70d1517 commit 4e73ff3

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,23 @@ public function execute()
9797
$cartData = $this->quantityProcessor->process($cartData);
9898
$quote = $this->checkoutSession->getQuote();
9999

100+
$response = [];
100101
foreach ($cartData as $itemId => $itemInfo) {
101102
$item = $quote->getItemById($itemId);
102103
$qty = isset($itemInfo['qty']) ? (double) $itemInfo['qty'] : 0;
103104
if ($item) {
104-
$this->updateItemQuantity($item, $qty);
105+
try {
106+
$this->updateItemQuantity($item, $qty);
107+
} catch (LocalizedException $e) {
108+
$response[] = [
109+
'error' => $e->getMessage(),
110+
'itemId' => $itemId
111+
];
112+
}
105113
}
106114
}
107115

108-
$this->jsonResponse();
109-
} catch (LocalizedException $e) {
110-
$this->jsonResponse($e->getMessage());
116+
$this->jsonResponse(count($response)? json_encode($response) : '');
111117
} catch (\Exception $e) {
112118
$this->logger->critical($e->getMessage());
113119
$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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ $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>

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

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

0 commit comments

Comments
 (0)