Skip to content

Commit 0e2bca6

Browse files
authored
Merge pull request #2971 from magento-mpi/port-2752
[MPI] Forward port #2689 #2722 #2752
2 parents f1154d4 + 574e1e7 commit 0e2bca6

File tree

53 files changed

+2650
-343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2650
-343
lines changed

app/code/Magento/Catalog/view/adminhtml/requirejs-config.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,18 @@ var config = {
1717
},
1818
deps: [
1919
'Magento_Catalog/catalog/product'
20-
]
20+
],
21+
config: {
22+
mixins: {
23+
'Magento_Catalog/js/components/use-parent-settings/select': {
24+
'Magento_Catalog/js/components/use-parent-settings/toggle-disabled-mixin': true
25+
},
26+
'Magento_Catalog/js/components/use-parent-settings/textarea': {
27+
'Magento_Catalog/js/components/use-parent-settings/toggle-disabled-mixin': true
28+
},
29+
'Magento_Catalog/js/components/use-parent-settings/single-checkbox': {
30+
'Magento_Catalog/js/components/use-parent-settings/toggle-disabled-mixin': true
31+
}
32+
}
33+
}
2134
};

app/code/Magento/Catalog/view/adminhtml/ui_component/category_form.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -452,34 +452,34 @@
452452
</checkbox>
453453
</formElements>
454454
</field>
455-
<field name="custom_design" sortOrder="180" formElement="select">
455+
<field name="custom_design" component="Magento_Catalog/js/components/use-parent-settings/select" sortOrder="180" formElement="select">
456456
<settings>
457457
<dataType>string</dataType>
458458
<label translate="true">Theme</label>
459459
<imports>
460-
<link name="disabled">${ $.parentName }.custom_use_parent_settings:checked</link>
460+
<link name="serviceDisabled">${ $.parentName }.custom_use_parent_settings:checked</link>
461461
</imports>
462462
</settings>
463463
</field>
464-
<field name="page_layout" sortOrder="190" formElement="select" class="Magento\Catalog\Ui\Component\Form\Field\Category\PageLayout">
464+
<field name="page_layout" sortOrder="190" formElement="select" component="Magento_Catalog/js/components/use-parent-settings/select" class="Magento\Catalog\Ui\Component\Form\Field\Category\PageLayout">
465465
<settings>
466466
<dataType>string</dataType>
467467
<label translate="true">Layout</label>
468468
<imports>
469-
<link name="disabled">${ $.parentName }.custom_use_parent_settings:checked</link>
469+
<link name="serviceDisabled">${ $.parentName }.custom_use_parent_settings:checked</link>
470470
</imports>
471471
</settings>
472472
</field>
473-
<field name="custom_layout_update" sortOrder="200" formElement="textarea">
473+
<field name="custom_layout_update" component="Magento_Catalog/js/components/use-parent-settings/textarea" sortOrder="200" formElement="textarea">
474474
<settings>
475475
<dataType>string</dataType>
476476
<label translate="true">Layout Update XML</label>
477477
<imports>
478-
<link name="disabled">ns = ${ $.ns }, index = custom_use_parent_settings :checked</link>
478+
<link name="serviceDisabled">${ $.parentName }.custom_use_parent_settings:checked</link>
479479
</imports>
480480
</settings>
481481
</field>
482-
<field name="custom_apply_to_products" component="Magento_Ui/js/form/element/single-checkbox" sortOrder="210" formElement="checkbox">
482+
<field name="custom_apply_to_products" component="Magento_Catalog/js/components/use-parent-settings/single-checkbox" sortOrder="210" formElement="checkbox">
483483
<argument name="data" xsi:type="array">
484484
<item name="config" xsi:type="array">
485485
<item name="default" xsi:type="number">0</item>
@@ -492,7 +492,7 @@
492492
<dataType>boolean</dataType>
493493
<label translate="true">Apply Design to Products</label>
494494
<imports>
495-
<link name="disabled">ns = ${ $.ns }, index = custom_use_parent_settings:checked</link>
495+
<link name="serviceDisabled">${ $.parentName }.custom_use_parent_settings:checked</link>
496496
</imports>
497497
</settings>
498498
<formElements>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/**
7+
* @api
8+
*/
9+
define([
10+
'Magento_Ui/js/form/element/select'
11+
], function (Component) {
12+
'use strict';
13+
14+
return Component;
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/**
7+
* @api
8+
*/
9+
define([
10+
'Magento_Ui/js/form/element/single-checkbox'
11+
], function (Component) {
12+
'use strict';
13+
14+
return Component;
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/**
7+
* @api
8+
*/
9+
define([
10+
'Magento_Ui/js/form/element/textarea'
11+
], function (Component) {
12+
'use strict';
13+
14+
return Component;
15+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'underscore'
8+
], function (_) {
9+
'use strict';
10+
11+
var mixin = {
12+
defaults: {
13+
imports: {
14+
toggleDisabled: '${ $.parentName }.custom_use_parent_settings:checked'
15+
},
16+
useParent: false,
17+
useDefaults: false
18+
},
19+
20+
/**
21+
* Disable form input if settings for parent section is used
22+
* or default value is applied.
23+
*
24+
* @param {Boolean} isUseParent
25+
*/
26+
toggleDisabled: function (isUseParent) {
27+
var disabled = this.useParent = isUseParent;
28+
29+
if (!disabled && !_.isUndefined(this.service)) {
30+
disabled = !!this.isUseDefault();
31+
}
32+
33+
this.saveUseDefaults();
34+
this.disabled(disabled);
35+
},
36+
37+
/**
38+
* Stores original state of the field.
39+
*/
40+
saveUseDefaults: function () {
41+
this.useDefaults = this.disabled();
42+
},
43+
44+
/** @inheritdoc */
45+
setInitialValue: function () {
46+
this._super();
47+
this.isUseDefault(this.useDefaults);
48+
49+
return this;
50+
},
51+
52+
/** @inheritdoc */
53+
toggleUseDefault: function (state) {
54+
this._super();
55+
this.disabled(state || this.useParent);
56+
}
57+
};
58+
59+
return function (target) {
60+
return target.extend(mixin);
61+
};
62+
});

app/code/Magento/Catalog/view/frontend/web/js/product/storage/storage-service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ define([
4747
* @param {*} data
4848
*/
4949
add: function (data) {
50-
if (!utils.compare(data, this.data()).equal) {
50+
if (!_.isEmpty(data) && !utils.compare(data, this.data()).equal) {
5151
this.data(_.extend(utils.copy(this.data()), data));
5252
}
5353
},
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Checkout\Controller\Cart;
9+
10+
use Magento\Checkout\Model\Cart\RequestQuantityProcessor;
11+
use Magento\Framework\App\Action\Context;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Checkout\Model\Session as CheckoutSession;
14+
use Magento\Framework\Serialize\Serializer\Json;
15+
use Magento\Framework\Data\Form\FormKey\Validator as FormKeyValidator;
16+
use Magento\Quote\Model\Quote\Item;
17+
use Psr\Log\LoggerInterface;
18+
19+
class UpdateItemQty extends \Magento\Framework\App\Action\Action
20+
{
21+
/**
22+
* @var RequestQuantityProcessor
23+
*/
24+
private $quantityProcessor;
25+
26+
/**
27+
* @var FormKeyValidator
28+
*/
29+
private $formKeyValidator;
30+
31+
/**
32+
* @var CheckoutSession
33+
*/
34+
private $checkoutSession;
35+
36+
/**
37+
* @var Json
38+
*/
39+
private $json;
40+
41+
/**
42+
* @var LoggerInterface
43+
*/
44+
private $logger;
45+
46+
/**
47+
* @param Context $context,
48+
* @param RequestQuantityProcessor $quantityProcessor
49+
* @param FormKeyValidator $formKeyValidator
50+
* @param CheckoutSession $checkoutSession
51+
* @param Json $json
52+
* @param LoggerInterface $logger
53+
*/
54+
public function __construct(
55+
Context $context,
56+
RequestQuantityProcessor $quantityProcessor,
57+
FormKeyValidator $formKeyValidator,
58+
CheckoutSession $checkoutSession,
59+
Json $json,
60+
LoggerInterface $logger
61+
) {
62+
$this->quantityProcessor = $quantityProcessor;
63+
$this->formKeyValidator = $formKeyValidator;
64+
$this->checkoutSession = $checkoutSession;
65+
$this->json = $json;
66+
$this->logger = $logger;
67+
parent::__construct($context);
68+
}
69+
70+
/**
71+
* @return void
72+
*/
73+
public function execute()
74+
{
75+
try {
76+
if (!$this->formKeyValidator->validate($this->getRequest())) {
77+
throw new LocalizedException(
78+
__('Something went wrong while saving the page. Please refresh the page and try again.')
79+
);
80+
}
81+
82+
$cartData = $this->getRequest()->getParam('cart');
83+
if (!is_array($cartData)) {
84+
throw new LocalizedException(
85+
__('Something went wrong while saving the page. Please refresh the page and try again.')
86+
);
87+
}
88+
89+
$cartData = $this->quantityProcessor->process($cartData);
90+
$quote = $this->checkoutSession->getQuote();
91+
92+
foreach ($cartData as $itemId => $itemInfo) {
93+
$item = $quote->getItemById($itemId);
94+
$qty = isset($itemInfo['qty']) ? (double)$itemInfo['qty'] : 0;
95+
if ($item) {
96+
$this->updateItemQuantity($item, $qty);
97+
}
98+
}
99+
100+
$this->jsonResponse();
101+
} catch (LocalizedException $e) {
102+
$this->jsonResponse($e->getMessage());
103+
} catch (\Exception $e) {
104+
$this->logger->critical($e->getMessage());
105+
$this->jsonResponse('Something went wrong while saving the page. Please refresh the page and try again.');
106+
}
107+
}
108+
109+
/**
110+
* Updates quote item quantity.
111+
*
112+
* @param Item $item
113+
* @param float $qty
114+
* @throws LocalizedException
115+
*/
116+
private function updateItemQuantity(Item $item, float $qty)
117+
{
118+
if ($qty > 0) {
119+
$item->setQty($qty);
120+
121+
if ($item->getHasError()) {
122+
throw new LocalizedException(__($item->getMessage()));
123+
}
124+
}
125+
}
126+
127+
/**
128+
* JSON response builder.
129+
*
130+
* @param string $error
131+
* @return void
132+
*/
133+
private function jsonResponse(string $error = '')
134+
{
135+
$this->getResponse()->representJson(
136+
$this->json->serialize($this->getResponseData($error))
137+
);
138+
}
139+
140+
/**
141+
* Returns response data.
142+
*
143+
* @param string $error
144+
* @return array
145+
*/
146+
private function getResponseData(string $error = ''): array
147+
{
148+
$response = [
149+
'success' => true,
150+
];
151+
152+
if (!empty($error)) {
153+
$response = [
154+
'success' => false,
155+
'error_message' => $error,
156+
];
157+
}
158+
159+
return $response;
160+
}
161+
}

0 commit comments

Comments
 (0)