Skip to content

fix(catalog): prefer the current category row for product position - #38

Merged
Seiger merged 1 commit into
Seiger:1.xfrom
arikc79:fix/product-position-in-category
Nov 17, 2026
Merged

Seiger merged 1 commit into
Seiger:1.xfrom
arikc79:fix/product-position-in-category

Conversation

@arikc79

@arikc79 arikc79 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

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() selects position with a subquery over s_product_category that matches the row of the current category OR the row with scope = primary, and then takes LIMIT 1 without 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:

->orderByRaw('(' . DB::getTablePrefix() . 's_product_category.category = ' . (int)$this->getCategoryId() . ') desc')

The table prefix is added by hand because orderByRaw does 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):

  • positions 1..N set for one category in reverse id order: the storefront list follows position ascending inside each availability group, before the patch it ignored it
  • all positions 0 (default): order unchanged
  • other sort modes (popularity, price, novelty) unchanged
  • php -l clean

Note: a storefront has to sort by position for this to be visible. The default controller sort is by availability and name, so the site template may need position before the name.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T14:32:34.469098Z 5490221 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/sCommerce.php
->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')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@Seiger
Seiger merged commit 8114ae1 into Seiger:1.x Nov 17, 2026
2 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.

2 participants