Skip to content

Added a category id exclusion option in the config #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ class Config
private const OCS_GENERAL_PRODUCT_ID_PATH = 'optimize_cache_size/general/product_id';
private const OCS_GENERAL_PRODUCT_SKU_PATH = 'optimize_cache_size/general/product_sku';
private const OCS_GENERAL_CATEGORY_ID_PATH = 'optimize_cache_size/general/category_id';
private const OCS_GENERAL_CATEGORY_ID_EXCLUSION_PATH = 'optimize_cache_size/general/category_id_exclusion';

public function __construct(
private ScopeConfigInterface $scopeConfig
) {
)
{
}

public function isModuleEnabled(int $store = 0): bool
{
return $this->scopeConfig->isSetFlag(
return $this->scopeConfig->isSetFlag(
self::OCS_GENERAL_IS_ENABLED_PATH,
ScopeInterface::SCOPE_STORE,
$store
Expand All @@ -32,7 +34,7 @@ public function isModuleEnabled(int $store = 0): bool

public function isRemoveProductIdHandlers(int $store = 0): bool
{
return $this->scopeConfig->isSetFlag(
return $this->scopeConfig->isSetFlag(
self::OCS_GENERAL_PRODUCT_ID_PATH,
ScopeInterface::SCOPE_STORE,
$store
Expand All @@ -41,7 +43,7 @@ public function isRemoveProductIdHandlers(int $store = 0): bool

public function isRemoveProductSkuHandlers(int $store = 0): bool
{
return $this->scopeConfig->isSetFlag(
return $this->scopeConfig->isSetFlag(
self::OCS_GENERAL_PRODUCT_SKU_PATH,
ScopeInterface::SCOPE_STORE,
$store
Expand All @@ -50,10 +52,19 @@ public function isRemoveProductSkuHandlers(int $store = 0): bool

public function isRemoveCategoryIdHandlers(int $store = 0): bool
{
return $this->scopeConfig->isSetFlag(
return $this->scopeConfig->isSetFlag(
self::OCS_GENERAL_CATEGORY_ID_PATH,
ScopeInterface::SCOPE_STORE,
$store
);
}

public function getCategoryIdExclusions(int $store = 0): array
{
return explode(',', $this->scopeConfig->getValue(
self::OCS_GENERAL_CATEGORY_ID_EXCLUSION_PATH,
ScopeInterface::SCOPE_STORE,
$store
) ?? '');
}
}
43 changes: 43 additions & 0 deletions Model/Config/Source/CategoryList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Vendic\OptimizeCacheSize\Model\Config\Source;

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\Data\OptionSourceInterface;

class CategoryList implements OptionSourceInterface
{
/**
* @var CollectionFactory
*/
private $categoryCollectionFactory;

public function __construct(
CollectionFactory $categoryCollectionFactory
) {
$this->categoryCollectionFactory = $categoryCollectionFactory;
}

/**
* Return array of ['value' => <category_id>, 'label' => <category_name>]
*
* @return array
*/
public function toOptionArray()
{
$collection = $this->categoryCollectionFactory->create();
$collection->addAttributeToSelect('name')
->addIsActiveFilter()
->addAttributeToSort('name', 'ASC');

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

return $options;
}
}
10 changes: 8 additions & 2 deletions Plugin/RemoveHandlersPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ public function afterAddHandle(
foreach ($handlers as $handler) {
if ($this->config->isRemoveCategoryIdHandlers()
&& str_contains($handler, self::CATEGORY_ID_HANDLER_STRING)) {
$result->removeHandle($handler);
continue;

$categoryID = str_replace(self::CATEGORY_ID_HANDLER_STRING, '', $handler);
$categoryIdExclusions = $this->config->getCategoryIdExclusions();

if(!in_array($categoryID, $categoryIdExclusions)){
$result->removeHandle($handler);
continue;
}
}
if ($this->config->isRemoveProductIdHandlers()
&& str_contains($handler, self::PRODUCT_ID_HANDLER_STRING)) {
Expand Down
10 changes: 10 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@
<field id="optimize_cache_size/general/enabled">1</field>
</depends>
</field>
<field id="category_id_exclusion" translate="label" type="multiselect" sortOrder="40" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Exclude Category IDs</label>
<source_model>Vendic\OptimizeCacheSize\Model\Config\Source\CategoryList</source_model>
<comment>Select all categories which should keep their id in the cache handle</comment>
<depends>
<field id="optimize_cache_size/general/enabled">1</field>
<field id="optimize_cache_size/general/category_id">1</field>
</depends>
</field>
</group>
</section>
</system>
Expand Down