Skip to content

Fix: recursive lookup templates#380

Open
ahuininga-orisha wants to merge 2 commits into
developfrom
fix/recursive-lookup-templates
Open

Fix: recursive lookup templates#380
ahuininga-orisha wants to merge 2 commits into
developfrom
fix/recursive-lookup-templates

Conversation

@ahuininga-orisha

Copy link
Copy Markdown
Collaborator

Summary

  • Fixes N+1 DB queries in TemplateFinder::forCategory (closes Performance impact because of category lookup in Model/Config/TemplateFinder.php #291). The original implementation recursively called $category->getParentCategory() to walk up the category tree, which internally calls CategoryRepository::get() — one DB query per ancestor level. On a PDP with a product in a deeply nested category this could trigger a dozen or more queries.
  • Replaced the recursive lookup with a single batch collection load. The fix uses Category::getPathIds() to extract all ancestor IDs from the already-loaded path string (e.g. 1/2/9/10/13) — no DB call. It then loads all ancestors in one query via CategoryCollectionFactory, selecting only the two relevant EAV attributes (tweakwise_{type}_template and tweakwise_{type}_group_code). The ancestors are then iterated from closest to furthest to preserve the original nearest-match behaviour.
  • Added declare(strict_types=1) and cleaned up imports as part of touching the file.

How to test

Scenario 1 — Ancestor template is inherited

  1. In the Magento admin, open a category that is a parent of a product's category (e.g. Women > Tops) and set a Tweakwise upsell or crosssell template on it. Leave the child category and product itself without a template.
  2. Open the PDP of a product in that child category.
  3. Verify the recommendation block renders using the template set on the ancestor category.
  4. Confirm in the Magento profiler / query log that the number of category queries is 1 (the batch load) rather than one per ancestor level.

Scenario 2 — Direct category template takes priority

  1. Set a template directly on the product's immediate category.
  2. Also set a different template on a parent category.
  3. Open the PDP and verify the recommendation block uses the template from the direct category, not the parent.

Scenario 3 — Product-level template takes priority over all categories

  1. Set a template directly on the product via its attribute.
  2. Also set a different template on its category and a parent category.
  3. Open the PDP and verify the recommendation block uses the product-level template.

Scenario 4 — Falls back to global default when nothing is set

  1. Ensure no template is set on the product, its category, or any ancestor.
  2. Open the PDP and verify recommendations still render using the global default template configured under Stores > Configuration > Tweakwise > Recommendations.

Comment thread Model/Config/TemplateFinder.php Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants