Skip to content

Commit

Permalink
Merge pull request #352 from SnowdogApps/feature/SMM-8
Browse files Browse the repository at this point in the history
SMM-8 Import categories by store view
  • Loading branch information
maartynaa authored Dec 16, 2024
2 parents 68fb62b + 6bc3682 commit 0495cd2
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 7 deletions.
41 changes: 41 additions & 0 deletions Plugin/Model/ResourceModel/Category/TreePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Snowdog\Menu\Plugin\Model\ResourceModel\Category;

use Magento\Catalog\Model\ResourceModel\Category\Tree;
use Magento\Framework\App\RequestInterface;

class TreePlugin
{
private RequestInterface $request;

public function __construct(
RequestInterface $request
) {
$this->request = $request;
}


/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeAddCollectionData(Tree $subject, $collection = null): array
{
$postData = $this->request->getPost();
$storeId = $postData['store_id'];

if (!isset($postData['store_id'])) {
return [$collection];
}

$collection->setProductStoreId(
$storeId
)->setStoreId(
$storeId
);

return [$collection];
}
}
40 changes: 34 additions & 6 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(true);

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

return $options;
}

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

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

return $options;
}

/**
* Retrieve tree of categories with attributes.
*
* @param int $storeId
* @param bool $toOptionArray
* @return array
* @throws LocalizedException
*/
Expand All @@ -60,7 +85,7 @@ private function retrieveCategories(int $storeId = 0, $toOptionArray = true): ar
$collection->addAttributeToSelect(['name', 'is_active', 'parent_id'])
->addFieldToFilter('level', 1)
->setStoreId($storeId);

$options = [];

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

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

ksort($options);

return $options;
}
}
6 changes: 6 additions & 0 deletions etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
</argument>
</arguments>
</virtualType>

<type name="Magento\Catalog\Model\ResourceModel\Category\Tree">
<plugin name="snowdog_menu_get_category_tree_by_sore_view"
type="Snowdog\Menu\Plugin\Model\ResourceModel\Category\TreePlugin"/>
</type>

<!--<type name="Snowdog\Menu\Ui\DataProvider\Menu\Form\MenuDataProvider">
<arguments>
<argument name="pool" xsi:type="object">Snowdog\Menu\Ui\DataProvider\Menu\Form\Modifier\Pool</argument>
Expand Down
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 0495cd2

Please sign in to comment.