Repository navigation
fix(catalog): prefer the current category row for product position - #38
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5490221133
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| ->orWhere('s_product_category.scope', 'primary'); | ||
| }) | ||
| // Prefer the row of the current category (manual order from admin) over the primary one | ||
| ->orderByRaw('(' . DB::getTablePrefix() . 's_product_category.category = ' . (int)$this->getCategoryId() . ') desc') |
There was a problem hiding this comment.
Quote the prefixed table identifier in the raw order clause
When an installation uses a table prefix containing characters that require quoting, such as shop-, this raw fragment is parsed as operators rather than as the s_product_category.category column; the query builder quotes the prefixed table in the FROM clause, but this expression bypasses that quoting, so every getProducts() call fails on that installation. Use the connection grammar to quote the prefixed identifier.
Useful? React with 👍 / 👎.
Problem
The manual product order set in the admin ("Sorting by position" tab of a category) is ignored on the storefront for many products.
sCommerce::getProducts()selectspositionwith a subquery overs_product_categorythat matches the row of the current category OR the row withscope = primary, and then takesLIMIT 1without any ordering. A product usually has both rows: the one of the current category (with the position set in the admin) and the primary one (position 0). Which of the two is returned depends on the physical row order, so for many products the primary row wins and the manual position is never used.Change
The subquery now orders the matched rows so that the row of the current category comes first and the primary row stays only as a fallback:
The table prefix is added by hand because
orderByRawdoes not prefix table names. The category id is cast to int.Files:
src/sCommerce.php(2 lines).Checked
On a local copy of a production database (Evolution CMS 3.5, sCommerce 1.4/1.5 code path):
positionascending inside each availability group, before the patch it ignored itphp -lcleanNote: a storefront has to sort by
positionfor this to be visible. The default controller sort is by availability and name, so the site template may needpositionbefore the name.