fix: category sort order breaks with preload enabled#4
Merged
SamJUK merged 1 commit intoJul 15, 2026
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes SAM-6: category sort order was ignored whenever category-grid preload was enabled.
The preload logic ran as an
afterExecuteplugin on the category controller, which force-loaded the product collection (getLoadedProductCollection()) beforeToolbar::setCollection()had applied the current sort order inListProduct::_beforeToHtml(). Once a collection is loaded, lateraddOrder/setOrdercalls 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
afterSetCollectionplugin onMagento\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
SetCollectionTestcovering: enabled/disabled config gates, preload from a collection (mirrors the post-order state), and the 4-image cap.samjuk/magento-ci-testing-env:2.4.8-p3-php8.3container used by CI, module required into a full Magento install via the local path repo.