klevu / metadata

Klevu Metadata Module for Magento 2
2 stars 1 forks source link

Skip product collection load for categories with display mode 'page' #21

Closed erlisdhima closed 1 year ago

erlisdhima commented 1 year ago

This change will skip loading product collection if you choose 'Static block only' under the Display Settings > Display Mode in the category settings.

This will drastically improve performance, espacially in websites with a huge number of products. As far as I understand the klevu metadata is interested in the products of a category only when they display in the front end. If not, it would be good to understand whats the purpose of the metadata.

sandipklevu commented 1 year ago

@erlisdhima Thanks for flagging this.

Patch in TXT extension is attached to skip page display mode. .PATCH file was not allowed to attach although it is a valid type. :(

klevu-metadata-1.4.1-skip-page-displaymode.txt

Change the extension from .txt to .patch

diff --git a/Provider/CategoryMetadataProvider.php b/Provider/CategoryMetadataProvider.php
index bb297bc..5a0256d 100644
--- a/Provider/CategoryMetadataProvider.php
+++ b/Provider/CategoryMetadataProvider.php
@@ -113,7 +113,9 @@ class CategoryMetadataProvider implements CategoryMetadataProviderInterface
         if (null === $productCollection && method_exists($category, 'getProductCollection')) {
             $productCollection = $category->getProductCollection();
         }
-
+        if ($category->getDisplayMode() === \Magento\Catalog\Model\Category::DM_PAGE) {
+            return [];
+        }
         if (!$productCollection) {
             return [];
         }

I have tested that the patch installs correctly on 1.4.1. Please test this on a none production env, it has NOT been through QA at our end.

This is a composer patch and should be installed as such. These changes are currently going through QA and should be first installed in a none production environment and thoroughly tested to ensure there are no issues.


Patch installation instructions

# Create the patches directory if not already exists
mkdir patches/composer -p

# Add the patch file
vi patches/composer/klevu-metadata-1.4.1-skip-page-displaymode.patch  # add the patch content provided

# Update composer.json to include patch information under "extra" section
vi composer.json

"extra": {
    "patches": {
        "klevu/module-metadata": {
            "Product metadata 1.4.1": "patches/composer/klevu-metadata-1.4.1-skip-page-displaymode.patch"
        }
    }
}

# Install composer patches if not already available
# Either https://github.com/vaimo/composer-patches
composer require vaimo/composer-patches

# or https://github.com/cweagans/composer-patches
composer require cweagans/composer-patches

# Apply the patch
composer -v install

# Update the composer lock file
composer update --lock
erlisdhima commented 1 year ago

Hi @sandipklevu ,

Appreciate sharing the patch! I will apply this and see if it gives the same results. Will try to get back within next week. Thanks!

sandipklevu commented 1 year ago

@erlisdhima I have moved the display mode check before the getProductCollection() which would certainly avoid the method call from the backend.

Here is the updated patch.

klevu-metadata-1.4.1-skip-page-displaymode.txt

erlisdhima commented 1 year ago

Thanks @sandipklevu ! I have applied the updated patch and it seems its working fine.

klevu commented 1 year ago

New Release Candidate created https://github.com/klevu/metadata/pull/22