Skip to content

fix: category sort order breaks with preload enabled#4

Merged
SamJUK merged 1 commit into
masterfrom
samjuk1999/sam-6-category-sort-order-breaks-with-preload-enabled
Jul 15, 2026
Merged

fix: category sort order breaks with preload enabled#4
SamJUK merged 1 commit into
masterfrom
samjuk1999/sam-6-category-sort-order-breaks-with-preload-enabled

Conversation

@SamJUK

@SamJUK SamJUK commented Jul 14, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes SAM-6: category sort order was ignored whenever category-grid preload was enabled.

The preload logic ran as an afterExecute plugin on the category controller, which force-loaded the product collection (getLoadedProductCollection()) before Toolbar::setCollection() had applied the current sort order in ListProduct::_beforeToHtml(). Once a collection is loaded, later addOrder/setOrder calls have no effect on the already-executed query — so sort silently reverted to default.

Per the potential solution in the ticket, moved the preload hook to an afterSetCollection plugin on Magento\Catalog\Block\Product\ProductList\Toolbar — order/paging are already applied to the collection by the time this plugin fires, so grabbing the first 4 images for preload no longer races the toolbar's sort.

Test plan

  • Added SetCollectionTest covering: enabled/disabled config gates, preload from a collection (mirrors the post-order state), and the 4-image cap.
  • Ran phpcs (Magento2 standard), phpstan (level 1), and full phpunit suite (45/45 passing) inside the samjuk/magento-ci-testing-env:2.4.8-p3-php8.3 container used by CI, module required into a full Magento install via the local path repo.

The preload plugin ran as an afterExecute on the category controller,
which forces the product collection to load (via getLoadedProductCollection)
before the Toolbar block has applied the current sort order in
_beforeToHtml. Once a collection is loaded, later addOrder/setOrder
calls from Toolbar::setCollection have no effect, so sort order broke
whenever category preload was enabled.

Move the hook to an afterSetCollection plugin on
Magento\Catalog\Block\Product\ProductList\Toolbar instead — order and
paging are already applied to the collection by the time this plugin
runs, so iterating it here to grab preload images no longer races the
toolbar's sort.
@SamJUK
SamJUK merged commit e9e9c7f into master Jul 15, 2026
18 of 19 checks passed
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.

1 participant