Skip to content

Add config to remove catalog_product_view_attribute_set_ layout handle #7

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
10 changes: 10 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Config
private const OCS_GENERAL_IS_ENABLED_PATH = 'optimize_cache_size/general/enabled';
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_PRODUCT_ATTRIBUTE_SET_PATH = 'optimize_cache_size/general/product_attribute_set';
private const OCS_GENERAL_CATEGORY_ID_PATH = 'optimize_cache_size/general/category_id';

public function __construct(
Expand Down Expand Up @@ -48,6 +49,15 @@ public function isRemoveProductSkuHandlers(int $store = 0): bool
);
}

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

public function isRemoveCategoryIdHandlers(int $store = 0): bool
{
return $this->scopeConfig->isSetFlag(
Expand Down
5 changes: 5 additions & 0 deletions Plugin/RemoveHandlersPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class RemoveHandlersPlugin

private const PRODUCT_ID_HANDLER_STRING = 'catalog_product_view_id_';
private const PRODUCT_SKU_HANDLER_STRING = 'catalog_product_view_sku_';
private const PRODUCT_ATTRIBUTE_SET_HANDLER_STRING = 'catalog_product_view_attribute_set_';
private const CATEGORY_ID_HANDLER_STRING = 'catalog_category_view_id_';

public function __construct(
Expand Down Expand Up @@ -45,6 +46,10 @@ public function afterAddHandle(
&& str_contains($handler, self::PRODUCT_SKU_HANDLER_STRING)) {
$result->removeHandle($handler);
}
if ($this->config->isRemoveProductAttributeSetHandlers()
&& str_contains($handler, self::PRODUCT_ATTRIBUTE_SET_HANDLER_STRING)) {
$result->removeHandle($handler);
}
}
return $result;
}
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Vendic_OptimizeCacheSize
Magento 2 extension that reduces the number of cache keys by removing `catalog_product_view_id_`, `catalog_product_view_sku_`, `catalog_category_view_id_`
Magento 2 extension that reduces the number of cache keys by removing `catalog_product_view_id_`, `catalog_product_view_sku_`, `catalog_product_view_attribute_set_`, `catalog_category_view_id_`
layout handles **by default**,

**This means that you loose the option to have product/category specific layout handles**, unless you re-enable them via the configuration.
Expand All @@ -14,7 +14,7 @@ composer require vendic/module-optimize-cache-size
## Related discussions
- [#189 Configuration option to disable product specific layout update handles ](https://github.com/magento/magento2/issues/189#issuecomment-698430804)
- [#102 New handles for the Category, Product and CMS pages](https://github.com/magento/magento2/issues/102)
- [#36244 Product layout based on attribute set](https://github.com/magento/magento2/pull/36244#issuecomment-2231106962) - We need to adjust this extension for Magento 2.4.8, since it's not possible to create specific layouts per attribute set. See [toonvd's comment](https://github.com/magento/magento2/pull/36244#issuecomment-2265233727)
- [#36244 Product layout based on attribute set](https://github.com/magento/magento2/pull/36244#issuecomment-2231106962)

## Results
Here's a some real life results from a Magento 2.4 instance with 7k products and 1.5k categories:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vendic/module-optimize-cache-size",
"description": "Magento 2 extension that reduces the number of cache keys by removing catalog_product_view_id_, catalog_product_view_sku_, catalog_category_view_id_ layout handles by default",
"description": "Magento 2 extension that reduces the number of cache keys by removing catalog_product_view_id_, catalog_product_view_sku_, catalog_product_view_attribute_set_, catalog_category_view_id_ layout handles by default",
"type": "magento2-module",
"require": {
"magento/framework": "*",
Expand Down
9 changes: 9 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
<field id="optimize_cache_size/general/enabled">1</field>
</depends>
</field>
<field id="product_attribute_set" translate="label" type="select" sortOrder="20" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Remove Product Attribute Set Handlers</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Removes catalog_product_view_attribute_set_ID handlers</comment>
<depends>
<field id="optimize_cache_size/general/enabled">1</field>
</depends>
</field>
<field id="category_id" translate="label" type="select" sortOrder="30" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Remove Category ID Handlers</label>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<enabled>1</enabled>
<product_id>1</product_id>
<product_sku>1</product_sku>
<product_attribute_set>1</product_attribute_set>
<category_id>1</category_id>
</general>
</optimize_cache_size>
Expand Down