Skip to content

Commit 70ba0ea

Browse files
author
Valeriy Nayda
authored
Merge pull request magento#559 from magento-engcom/451_assigned_sources_disabled
magento#451 disabled sources tab content on manage_stock update
2 parents 5cade93 + 1a0e59d commit 70ba0ea

File tree

5 files changed

+147
-3
lines changed

5 files changed

+147
-3
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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\InventoryCatalog\Model;
9+
10+
use Magento\CatalogInventory\Model\Configuration;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\InventoryCatalog\Api\DefaultStockProviderInterface;
13+
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
14+
15+
/**
16+
* Check source items should be managed for given product sku
17+
*/
18+
class CanManageSourceItemsBySku
19+
{
20+
/**
21+
* Provides manage stock global config value.
22+
*
23+
* @var ScopeConfigInterface
24+
*/
25+
private $config;
26+
27+
/**
28+
* Provides default stock id for current website in order to get correct stock configuration for product.
29+
*
30+
* @var DefaultStockProviderInterface
31+
*/
32+
private $defaultStockProvider;
33+
34+
/**
35+
* Provides stock item configuration for given product sku.
36+
*
37+
* @var GetStockItemConfigurationInterface
38+
*/
39+
private $getStockItemConfiguration;
40+
41+
/**
42+
* @param ScopeConfigInterface $config
43+
* @param GetStockItemConfigurationInterface $getStockItemConfiguration
44+
* @param DefaultStockProviderInterface $defaultStockProvider
45+
*/
46+
public function __construct(
47+
ScopeConfigInterface $config,
48+
GetStockItemConfigurationInterface $getStockItemConfiguration,
49+
DefaultStockProviderInterface $defaultStockProvider
50+
) {
51+
$this->config = $config;
52+
$this->defaultStockProvider = $defaultStockProvider;
53+
$this->getStockItemConfiguration = $getStockItemConfiguration;
54+
}
55+
56+
/**
57+
* @param string $sku Sku can be null if product is new
58+
* @return bool
59+
*/
60+
public function execute(string $sku = null): bool
61+
{
62+
if (null !== $sku) {
63+
$stockId = $this->defaultStockProvider->getId();
64+
$itemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
65+
66+
if (null !== $itemConfiguration) {
67+
return $itemConfiguration->isUseConfigManageStock()
68+
? (bool)$this->config->getValue(Configuration::XML_PATH_MANAGE_STOCK)
69+
: $itemConfiguration->isManageStock();
70+
}
71+
}
72+
return (bool)$this->config->getValue(Configuration::XML_PATH_MANAGE_STOCK);
73+
}
74+
}

app/code/Magento/InventoryCatalog/Ui/DataProvider/Product/Form/Modifier/SourceItems.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77

88
namespace Magento\InventoryCatalog\Ui\DataProvider\Product\Form\Modifier;
99

10-
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
1110
use Magento\Catalog\Model\Locator\LocatorInterface;
11+
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
1212
use Magento\Framework\App\ResourceConnection;
1313
use Magento\Inventory\Model\ResourceModel\Source as SourceResourceModel;
1414
use Magento\Inventory\Model\ResourceModel\SourceItem\Collection;
1515
use Magento\Inventory\Model\ResourceModel\SourceItem\CollectionFactory;
1616
use Magento\InventoryApi\Api\Data\SourceInterface;
1717
use Magento\InventoryApi\Api\Data\SourceItemInterface;
18+
use Magento\InventoryCatalog\Model\CanManageSourceItemsBySku;
1819
use Magento\InventoryCatalog\Model\IsSingleSourceModeInterface;
1920
use Magento\InventoryConfiguration\Model\IsSourceItemsAllowedForProductTypeInterface;
2021

@@ -48,25 +49,33 @@ class SourceItems extends AbstractModifier
4849
*/
4950
private $resourceConnection;
5051

52+
/**
53+
* @var CanManageSourceItemsBySku
54+
*/
55+
private $canManageSourceItemsBySku;
56+
5157
/**
5258
* @param IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType
5359
* @param IsSingleSourceModeInterface $isSingleSourceMode
5460
* @param LocatorInterface $locator
5561
* @param CollectionFactory $sourceItemCollectionFactory
5662
* @param ResourceConnection $resourceConnection
63+
* @param CanManageSourceItemsBySku $canManageSourceItemsBySku
5764
*/
5865
public function __construct(
5966
IsSourceItemsAllowedForProductTypeInterface $isSourceItemsAllowedForProductType,
6067
IsSingleSourceModeInterface $isSingleSourceMode,
6168
LocatorInterface $locator,
6269
CollectionFactory $sourceItemCollectionFactory,
63-
ResourceConnection $resourceConnection
70+
ResourceConnection $resourceConnection,
71+
CanManageSourceItemsBySku $canManageSourceItemsBySku
6472
) {
6573
$this->isSourceItemsAllowedForProductType = $isSourceItemsAllowedForProductType;
6674
$this->isSingleSourceMode = $isSingleSourceMode;
6775
$this->locator = $locator;
6876
$this->sourceItemCollectionFactory = $sourceItemCollectionFactory;
6977
$this->resourceConnection = $resourceConnection;
78+
$this->canManageSourceItemsBySku = $canManageSourceItemsBySku;
7079
}
7180

7281
/**
@@ -128,6 +137,7 @@ public function modifyMeta(array $meta)
128137
return $meta;
129138
}
130139

140+
$canMangeSourceItems = $this->canManageSourceItemsBySku->execute($product->getSku());
131141
$meta['sources'] = [
132142
'arguments' => [
133143
'data' => [
@@ -136,7 +146,32 @@ public function modifyMeta(array $meta)
136146
],
137147
],
138148
],
149+
'children' => [
150+
'assign_sources_container' => [
151+
'children' => [
152+
'assign_sources_button' => [
153+
'arguments' => [
154+
'data' => [
155+
'config' => [
156+
'visible' => $canMangeSourceItems,
157+
],
158+
],
159+
],
160+
],
161+
],
162+
],
163+
'assigned_sources' => [
164+
'arguments' => [
165+
'data' => [
166+
'config' => [
167+
'visible' => $canMangeSourceItems,
168+
],
169+
],
170+
],
171+
],
172+
],
139173
];
174+
140175
return $meta;
141176
}
142177
}

app/code/Magento/InventoryCatalog/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"magento/module-catalog-inventory": "100.3.*",
99
"magento/module-inventory": "100.0.*",
1010
"magento/module-inventory-api": "100.0.*",
11+
"magento/module-inventory-configuration-api": "100.0.*",
1112
"magento/module-ui": "100.0.*",
1213
"magento/module-store": "100.3.*",
1314
"magento/module-inventory-configuration": "100.0.*",

app/code/Magento/InventoryCatalog/view/adminhtml/ui_component/product_form.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
9-
<fieldset name="sources" sortOrder="5">
9+
<fieldset name="sources" sortOrder="5" component="Magento_InventoryCatalog/js/product/form/components/source-items">
1010
<settings>
1111
<label translate="true">Sources</label>
1212
<collapsible>true</collapsible>
@@ -37,6 +37,9 @@
3737
<item name="additionalForGroup" xsi:type="boolean">true</item>
3838
<item name="additionalClasses" xsi:type="string">admin__field-medium</item>
3939
<item name="dataScope" xsi:type="string"/>
40+
<item name="imports" xsi:type="array">
41+
<item name="visible" xsi:type="string">${ $.provider }:data.product.stock_data.manage_stock</item>
42+
</item>
4043
</item>
4144
</argument>
4245
</container>
@@ -85,6 +88,9 @@
8588
<param name="enabled" xsi:type="boolean">false</param>
8689
</dndConfig>
8790
<columnsHeader>true</columnsHeader>
91+
<imports>
92+
<link name="visible">${ $.provider }:data.product.stock_data.manage_stock</link>
93+
</imports>
8894
</settings>
8995
<container name="record" component="Magento_Ui/js/dynamic-rows/record">
9096
<argument name="data" xsi:type="array">
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'Magento_Ui/js/form/components/fieldset'
8+
], function (Fieldset) {
9+
'use strict';
10+
11+
return Fieldset.extend({
12+
defaults: {
13+
imports: {
14+
onStockChange: '${ $.provider }:data.product.stock_data.manage_stock'
15+
}
16+
},
17+
18+
/**
19+
* Disable all child elements if manage stock is zero
20+
* @param {Integer} canManageStock
21+
*/
22+
onStockChange: function (canManageStock) {
23+
if (canManageStock === 0) {
24+
this.delegate('disabled', true);
25+
}
26+
}
27+
});
28+
});

0 commit comments

Comments
 (0)