Skip to content

Commit

Permalink
SMM-8 Get categories by store id
Browse files Browse the repository at this point in the history
  • Loading branch information
Martyna Maciejewska committed Nov 28, 2024
1 parent 6c74665 commit 3ac5ea2
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 8 deletions.
41 changes: 34 additions & 7 deletions Ui/Component/Menu/Form/Element/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\Collection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\StoreManagerInterface;

class Categories implements ArrayInterface
{
Expand All @@ -16,12 +17,19 @@ class Categories implements ArrayInterface
*/
private $categoryCollectionFactory;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param CategoryCollectionFactory $categoryCollectionFactory
*/
public function __construct(
StoreManagerInterface $storeManager,
CategoryCollectionFactory $categoryCollectionFactory
) {
$this->storeManager = $storeManager;
$this->categoryCollectionFactory = $categoryCollectionFactory;
}

Expand All @@ -32,7 +40,15 @@ public function __construct(
*/
public function toOptionArray(): array
{
return $this->retrieveCategories();
$options = [];
$stores = $this->storeManager->getStores();

foreach ($stores as $store) {
$categories = $this->retrieveCategories((string) $store->getId());
$options = array_merge($options, $categories);
}

return $options;
}

/**
Expand All @@ -42,7 +58,15 @@ public function toOptionArray(): array
*/
public function toArray(): array
{
return $this->retrieveCategories(0, false);
$options = [];
$stores = $this->storeManager->getStores();

foreach ($stores as $store) {
$categories = $this->retrieveCategories((string) $store->getId(), false);
$options = array_merge($options, $categories);
}

return $options;
}

/**
Expand All @@ -52,15 +76,15 @@ public function toArray(): array
* @return array
* @throws LocalizedException
*/
private function retrieveCategories(int $storeId = 0, $toOptionArray = true): array
private function retrieveCategories(string $storeId = '0', $toOptionArray = true): array
{
/* @var $collection Collection */
$collection = $this->categoryCollectionFactory->create();

$collection->addAttributeToSelect(['name', 'is_active', 'parent_id'])
->addFieldToFilter('level', 1)
->setStoreId($storeId);

$options = [];

foreach ($collection as $rootCategory) {
Expand All @@ -75,7 +99,8 @@ private function retrieveCategories(int $storeId = 0, $toOptionArray = true): ar
if ($toOptionArray) {
$options[] = [
'label' => $rootCategory->getName(),
'value' => $rootCategory->getId()
'value' => $rootCategory->getId(),
'store_id' => $storeId,
];
} else {
$options[$rootCategory->getId()] = $rootCategory->getName();
Expand All @@ -86,7 +111,8 @@ private function retrieveCategories(int $storeId = 0, $toOptionArray = true): ar
if ($toOptionArray) {
$groupedOptions[] = [
'label' => $category->getName(),
'value' => $category->getId()
'value' => $category->getId(),
'store_id' => $storeId,
];
} else {
$options[$category->getId()] = $category->getName();
Expand All @@ -96,7 +122,8 @@ private function retrieveCategories(int $storeId = 0, $toOptionArray = true): ar
if ($toOptionArray && $groupedOptions) {
$options[] = [
'label' => __('Sub categories'),
'value' => $groupedOptions
'value' => $groupedOptions,
'store_id' => $storeId,
];
}
}
Expand Down
40 changes: 40 additions & 0 deletions Ui/Component/Menu/Form/Element/StoreView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Snowdog\Menu\Ui\Component\Menu\Form\Element;

use Magento\Framework\Option\ArrayInterface;
use Magento\Store\Model\StoreManagerInterface;

class StoreView implements ArrayInterface
{
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param StoreManagerInterface $storeManager
*/
public function __construct(
StoreManagerInterface $storeManager
) {
$this->storeManager = $storeManager;
}

public function toOptionArray(): array
{
$stores = $this->storeManager->getStores();

$options = [];
foreach ($stores as $store) {
$options[] = [
'label' => $store->getName(),
'value' => $store->getId(),
];
}

return $options;
}
}
17 changes: 17 additions & 0 deletions view/adminhtml/ui_component/snowmenu_menu_form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@
<settings>
<label>Import from categories tree</label>
</settings>
<field name="store_id" formElement="select">
<settings>
<dataType>int</dataType>
<label translate="true">Store View</label>
</settings>
<formElements>
<select>
<settings>
<options class="Snowdog\Menu\Ui\Component\Menu\Form\Element\StoreView"/>
</settings>
</select>
</formElements>
</field>
<field name="category_id" formElement="select">
<settings>
<dataType>int</dataType>
Expand All @@ -196,6 +209,10 @@
<formElements>
<select>
<settings>
<filterBy>
<field>store_id</field>
<target>${ $.provider }:${ $.parentScope }.store_id</target>
</filterBy>
<options class="Snowdog\Menu\Ui\Component\Menu\Form\Element\Categories"/>
</settings>
</select>
Expand Down
4 changes: 3 additions & 1 deletion view/adminhtml/web/js/mixins/modal-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ define([
form = registry.get('snowmenu_menu_form.data_source'),
vueApp = registry.get('vueApp'),
categoryId = parseInt(form.data.category_id),
storeId = parseInt(form.data.store_id),
depth = parseInt(form.data.depth),
adminPath = window.location.pathname.split('/snowmenu')[0]

Expand All @@ -42,7 +43,8 @@ define([
url: adminPath + '/snowmenu/menu/importCategories',
data: {
category_id: categoryId,
depth: depth
depth: depth,
store_id: storeId,
},
type: 'POST',
dataType: 'json',
Expand Down

0 comments on commit 3ac5ea2

Please sign in to comment.