Skip to content

Commit 0495cd2

Browse files
authored
Merge pull request SnowdogApps#352 from SnowdogApps/feature/SMM-8
SMM-8 Import categories by store view
2 parents 68fb62b + 6bc3682 commit 0495cd2

File tree

6 files changed

+143
-7
lines changed

6 files changed

+143
-7
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Snowdog\Menu\Plugin\Model\ResourceModel\Category;
6+
7+
use Magento\Catalog\Model\ResourceModel\Category\Tree;
8+
use Magento\Framework\App\RequestInterface;
9+
10+
class TreePlugin
11+
{
12+
private RequestInterface $request;
13+
14+
public function __construct(
15+
RequestInterface $request
16+
) {
17+
$this->request = $request;
18+
}
19+
20+
21+
/**
22+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
23+
*/
24+
public function beforeAddCollectionData(Tree $subject, $collection = null): array
25+
{
26+
$postData = $this->request->getPost();
27+
$storeId = $postData['store_id'];
28+
29+
if (!isset($postData['store_id'])) {
30+
return [$collection];
31+
}
32+
33+
$collection->setProductStoreId(
34+
$storeId
35+
)->setStoreId(
36+
$storeId
37+
);
38+
39+
return [$collection];
40+
}
41+
}

Ui/Component/Menu/Form/Element/Categories.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
99
use Magento\Catalog\Model\ResourceModel\Category\Collection;
1010
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Store\Model\StoreManagerInterface;
1112

1213
class Categories implements ArrayInterface
1314
{
@@ -16,12 +17,19 @@ class Categories implements ArrayInterface
1617
*/
1718
private $categoryCollectionFactory;
1819

20+
/**
21+
* @var StoreManagerInterface
22+
*/
23+
private $storeManager;
24+
1925
/**
2026
* @param CategoryCollectionFactory $categoryCollectionFactory
2127
*/
2228
public function __construct(
29+
StoreManagerInterface $storeManager,
2330
CategoryCollectionFactory $categoryCollectionFactory
2431
) {
32+
$this->storeManager = $storeManager;
2533
$this->categoryCollectionFactory = $categoryCollectionFactory;
2634
}
2735

@@ -32,7 +40,15 @@ public function __construct(
3240
*/
3341
public function toOptionArray(): array
3442
{
35-
return $this->retrieveCategories();
43+
$options = [];
44+
$stores = $this->storeManager->getStores(true);
45+
46+
foreach ($stores as $store) {
47+
$categories = $this->retrieveCategories((int) $store->getId());
48+
$options = array_merge($options, $categories);
49+
}
50+
51+
return $options;
3652
}
3753

3854
/**
@@ -42,13 +58,22 @@ public function toOptionArray(): array
4258
*/
4359
public function toArray(): array
4460
{
45-
return $this->retrieveCategories(0, false);
61+
$options = [];
62+
$stores = $this->storeManager->getStores(true);
63+
64+
foreach ($stores as $store) {
65+
$categories = $this->retrieveCategories((int) $store->getId(), false);
66+
$options = array_merge($options, $categories);
67+
}
68+
69+
return $options;
4670
}
4771

4872
/**
4973
* Retrieve tree of categories with attributes.
5074
*
5175
* @param int $storeId
76+
* @param bool $toOptionArray
5277
* @return array
5378
* @throws LocalizedException
5479
*/
@@ -60,7 +85,7 @@ private function retrieveCategories(int $storeId = 0, $toOptionArray = true): ar
6085
$collection->addAttributeToSelect(['name', 'is_active', 'parent_id'])
6186
->addFieldToFilter('level', 1)
6287
->setStoreId($storeId);
63-
88+
6489
$options = [];
6590

6691
foreach ($collection as $rootCategory) {
@@ -75,7 +100,8 @@ private function retrieveCategories(int $storeId = 0, $toOptionArray = true): ar
75100
if ($toOptionArray) {
76101
$options[] = [
77102
'label' => $rootCategory->getName(),
78-
'value' => $rootCategory->getId()
103+
'value' => $rootCategory->getId(),
104+
'store_id' => (string) $storeId,
79105
];
80106
} else {
81107
$options[$rootCategory->getId()] = $rootCategory->getName();
@@ -86,7 +112,8 @@ private function retrieveCategories(int $storeId = 0, $toOptionArray = true): ar
86112
if ($toOptionArray) {
87113
$groupedOptions[] = [
88114
'label' => $category->getName(),
89-
'value' => $category->getId()
115+
'value' => $category->getId(),
116+
'store_id' => (string) $storeId,
90117
];
91118
} else {
92119
$options[$category->getId()] = $category->getName();
@@ -96,7 +123,8 @@ private function retrieveCategories(int $storeId = 0, $toOptionArray = true): ar
96123
if ($toOptionArray && $groupedOptions) {
97124
$options[] = [
98125
'label' => __('Sub categories'),
99-
'value' => $groupedOptions
126+
'value' => $groupedOptions,
127+
'store_id' => (string) $storeId,
100128
];
101129
}
102130
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Snowdog\Menu\Ui\Component\Menu\Form\Element;
6+
7+
use Magento\Framework\Option\ArrayInterface;
8+
use Magento\Store\Model\StoreManagerInterface;
9+
10+
class StoreView implements ArrayInterface
11+
{
12+
/**
13+
* @var StoreManagerInterface
14+
*/
15+
private $storeManager;
16+
17+
/**
18+
* @param StoreManagerInterface $storeManager
19+
*/
20+
public function __construct(
21+
StoreManagerInterface $storeManager
22+
) {
23+
$this->storeManager = $storeManager;
24+
}
25+
26+
public function toOptionArray(): array
27+
{
28+
$stores = $this->storeManager->getStores(true);
29+
30+
$options = [];
31+
foreach ($stores as $store) {
32+
$options[$store->getId()] = [
33+
'label' => $store->getName(),
34+
'value' => $store->getId(),
35+
];
36+
}
37+
38+
ksort($options);
39+
40+
return $options;
41+
}
42+
}

etc/adminhtml/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
</argument>
1212
</arguments>
1313
</virtualType>
14+
15+
<type name="Magento\Catalog\Model\ResourceModel\Category\Tree">
16+
<plugin name="snowdog_menu_get_category_tree_by_sore_view"
17+
type="Snowdog\Menu\Plugin\Model\ResourceModel\Category\TreePlugin"/>
18+
</type>
19+
1420
<!--<type name="Snowdog\Menu\Ui\DataProvider\Menu\Form\MenuDataProvider">
1521
<arguments>
1622
<argument name="pool" xsi:type="object">Snowdog\Menu\Ui\DataProvider\Menu\Form\Modifier\Pool</argument>

view/adminhtml/ui_component/snowmenu_menu_form.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,19 @@
188188
<settings>
189189
<label>Import from categories tree</label>
190190
</settings>
191+
<field name="store_id" formElement="select">
192+
<settings>
193+
<dataType>int</dataType>
194+
<label translate="true">Store View</label>
195+
</settings>
196+
<formElements>
197+
<select>
198+
<settings>
199+
<options class="Snowdog\Menu\Ui\Component\Menu\Form\Element\StoreView"/>
200+
</settings>
201+
</select>
202+
</formElements>
203+
</field>
191204
<field name="category_id" formElement="select">
192205
<settings>
193206
<dataType>int</dataType>
@@ -196,6 +209,10 @@
196209
<formElements>
197210
<select>
198211
<settings>
212+
<filterBy>
213+
<field>store_id</field>
214+
<target>${ $.provider }:${ $.parentScope }.store_id</target>
215+
</filterBy>
199216
<options class="Snowdog\Menu\Ui\Component\Menu\Form\Element\Categories"/>
200217
</settings>
201218
</select>

view/adminhtml/web/js/mixins/modal-mixin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ define([
3434
form = registry.get('snowmenu_menu_form.data_source'),
3535
vueApp = registry.get('vueApp'),
3636
categoryId = parseInt(form.data.category_id),
37+
storeId = parseInt(form.data.store_id),
3738
depth = parseInt(form.data.depth),
3839
adminPath = window.location.pathname.split('/snowmenu')[0]
3940

@@ -42,7 +43,8 @@ define([
4243
url: adminPath + '/snowmenu/menu/importCategories',
4344
data: {
4445
category_id: categoryId,
45-
depth: depth
46+
depth: depth,
47+
store_id: storeId,
4648
},
4749
type: 'POST',
4850
dataType: 'json',

0 commit comments

Comments
 (0)