Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix position sort with flat tables when no category filter is applied #2637

Merged

Conversation

elidrissidev
Copy link
Member

@elidrissidev elidrissidev commented Oct 1, 2022

Description (*)

This PR will fix the issue when you sort the Product collection by position with flat tables ON, and you either didn't set the Category filter, or the Category doesn't exist (getId() returns null). The issue was what seems to be a redundant ORDER BY clause being added with cat_index_position (alias of catalog_category_product_index.position), which is only JOINed if the Category exists. There is already an ORDER BY clause being added below the removed code, with the condition of Category filter being present, so this shouldn't cause any change of behavior.

Fixed Issues (if relevant)

  1. Fixes Sort by position does not work #2511

Manual testing scenarios (*)

// Make sure you're not on Admin store, as flat tables is only used for frontend
Mage::app()->setCurrentStore(1);

Mage::getResourceModel('catalog/product_collection')
  // Either give it a non-existent category or omit the filter
  ->addCategoryFilter(Mage::getModel('catalog/category')->load(1234))
  ->addAttributeToSort('position')
  ->load();

This snipped will throw the below error before this patch:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cat_index_position' in 'order clause', query was: SELECT `e`.`entity_id`, `e`.`type_id`, `e`.`attribute_set_id` FROM `catalog_product_flat_1` AS `e` WHERE (e.status = 1) ORDER BY `cat_index_position` ASC, `e`.`entity_id` ASC"

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All automated tests passed successfully (all builds are green)
  • Add yourself to contributors list

@github-actions github-actions bot added the Component: Catalog Relates to Mage_Catalog label Oct 1, 2022
Copy link
Contributor

@luigifab luigifab left a comment

Choose a reason for hiding this comment

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

For me it's working with a local env, but we don't use sort by position on our store.

@fballiano fballiano merged commit 5a87abc into OpenMage:1.9.4.x Feb 16, 2023
@fballiano
Copy link
Contributor

merged and v20ed

@elidrissidev elidrissidev deleted the fix/product-collection-position-sort branch February 16, 2023 15:38
elidrissidev added a commit to elidrissidev/magento-lts that referenced this pull request Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Catalog Relates to Mage_Catalog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sort by position does not work
3 participants