Skip to content

Commit 9920900

Browse files
committed
Show product alerts in admin product detail
1 parent cb93fe5 commit 9920900

File tree

2 files changed

+177
-0
lines changed
  • app/code/Magento/Catalog

2 files changed

+177
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Ui\DataProvider\Product\Form\Modifier;
8+
9+
use Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Alerts\Price;
10+
use Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Alerts\Stock;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\View\LayoutFactory;
13+
use Magento\Store\Model\ScopeInterface;
14+
use Magento\Ui\Component\Form\Fieldset;
15+
16+
class Alerts extends AbstractModifier
17+
{
18+
const DATA_SCOPE = 'data';
19+
const DATA_SCOPE_STOCK = 'stock';
20+
const DATA_SCOPE_PRICE = 'price';
21+
22+
/**
23+
* @var string
24+
*/
25+
private static $previousGroup = 'related';
26+
27+
/**
28+
* @var int
29+
*/
30+
private static $sortOrder = 110;
31+
32+
/**
33+
* @var ScopeConfigInterface
34+
*/
35+
private $scopeConfig;
36+
37+
/**
38+
* @var LayoutFactory
39+
*/
40+
private $layoutFactory;
41+
42+
43+
/**
44+
* Alerts constructor.
45+
* @param ScopeConfigInterface $scopeConfig
46+
* @param LayoutFactory $layoutFactory
47+
*/
48+
public function __construct(
49+
ScopeConfigInterface $scopeConfig,
50+
LayoutFactory $layoutFactory
51+
) {
52+
$this->scopeConfig = $scopeConfig;
53+
$this->layoutFactory = $layoutFactory;
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
* @since 101.0.0
59+
*/
60+
public function modifyData(array $data)
61+
{
62+
return $data;
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
* @since 101.0.0
68+
*/
69+
public function modifyMeta(array $meta)
70+
{
71+
if (!$this->canShowTab()) {
72+
return $meta;
73+
}
74+
75+
$meta = array_replace_recursive(
76+
$meta,
77+
[
78+
'alerts' => [
79+
'arguments' => [
80+
'data' => [
81+
'config' => [
82+
'additionalClasses' => 'admin__fieldset-section',
83+
'label' => __('Product Alerts'),
84+
'collapsible' => true,
85+
'componentType' => Fieldset::NAME,
86+
'dataScope' => static::DATA_SCOPE,
87+
'sortOrder' =>
88+
$this->getNextGroupSortOrder(
89+
$meta,
90+
self::$previousGroup,
91+
self::$sortOrder
92+
),
93+
],
94+
],
95+
],
96+
'children' => [
97+
static::DATA_SCOPE_STOCK => $this->getAlertStockFieldset(),
98+
static::DATA_SCOPE_PRICE => $this->getAlertPriceFieldset()
99+
],
100+
],
101+
]
102+
);
103+
104+
return $meta;
105+
}
106+
107+
/**
108+
* @return bool
109+
*/
110+
private function canShowTab()
111+
{
112+
$alertPriceAllow = $this->scopeConfig->getValue(
113+
'catalog/productalert/allow_price',
114+
ScopeInterface::SCOPE_STORE
115+
);
116+
$alertStockAllow = $this->scopeConfig->getValue(
117+
'catalog/productalert/allow_stock',
118+
ScopeInterface::SCOPE_STORE
119+
);
120+
121+
return ($alertPriceAllow || $alertStockAllow);
122+
}
123+
124+
/**
125+
* Prepares config for the alert stock products fieldset
126+
* @return array
127+
*/
128+
private function getAlertStockFieldset()
129+
{
130+
return [
131+
'arguments' => [
132+
'data' => [
133+
'config' => [
134+
'label' => __('Alert stock'),
135+
'componentType' => 'container',
136+
'component' => 'Magento_Ui/js/form/components/html',
137+
'additionalClasses' => 'admin__fieldset-note',
138+
'content' =>
139+
'<h4>' . __('Alert Stock') . '</h4>' .
140+
$this->layoutFactory->create()->createBlock(
141+
Stock::class
142+
)->toHtml(),
143+
]
144+
]
145+
]
146+
];
147+
}
148+
149+
/**
150+
* Prepares config for the alert price products fieldset
151+
* @return array
152+
*/
153+
private function getAlertPriceFieldset()
154+
{
155+
return [
156+
'arguments' => [
157+
'data' => [
158+
'config' => [
159+
'label' => __('Alert price'),
160+
'componentType' => 'container',
161+
'component' => 'Magento_Ui/js/form/components/html',
162+
'additionalClasses' => 'admin__fieldset-note',
163+
'content' =>
164+
'<h4>' . __('Alert Price') . '</h4>' .
165+
$this->layoutFactory->create()->createBlock(
166+
Price::class
167+
)->toHtml(),
168+
]
169+
]
170+
]
171+
];
172+
}
173+
}

app/code/Magento/Catalog/etc/adminhtml/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@
143143
<item name="class" xsi:type="string">Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Attributes</item>
144144
<item name="sortOrder" xsi:type="number">120</item>
145145
</item>
146+
<item name="alerts" xsi:type="array">
147+
<item name="class" xsi:type="string">Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Alerts</item>
148+
<item name="sortOrder" xsi:type="number">130</item>
149+
</item>
146150
<item name="advanced-pricing-tier-price-type" xsi:type="array">
147151
<item name="class" xsi:type="string">Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\TierPrice</item>
148152
<item name="sortOrder" xsi:type="number">150</item>

0 commit comments

Comments
 (0)