Skip to content

Added a filter to sort categories by position by default #29301

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

Merged
merged 19 commits into from
Aug 6, 2020
Merged

Added a filter to sort categories by position by default #29301

merged 19 commits into from
Aug 6, 2020

Conversation

Dnyomo
Copy link

@Dnyomo Dnyomo commented Jul 28, 2020

Description (*)

This fixed a bug where the default GraphQL Category method does not sort by category position. By adding this filter results are returned in the correct order.

Related Pull Requests

magento/catalog-storefront#173

Fixed Issues (if relevant)

  1. Fixes Default sort order  catalog-storefront#104

Manual testing scenarios (*)

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented Jul 28, 2020

Hi @Dnyomo. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests

You can find more information about the builds here

ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review.

For more details, please, review the Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@Dnyomo
Copy link
Author

Dnyomo commented Jul 28, 2020

@magento run WebAPI Tests

@Dnyomo
Copy link
Author

Dnyomo commented Jul 28, 2020

@magento run WebAPI Tests

@@ -71,6 +72,7 @@ public function getResult(array $criteria, StoreInterface $store)
$categoryIds = [];
$criteria[Filter::ARGUMENT_NAME] = $this->formatMatchFilters($criteria['filters'], $store);
$criteria[Filter::ARGUMENT_NAME][CategoryInterface::KEY_IS_ACTIVE] = ['eq' => 1];
$criteria[Sort::ARGUMENT_NAME][CategoryInterface::KEY_POSITION] = ['ASC'];
Copy link
Member

Choose a reason for hiding this comment

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

@Dnyomo can you please verify and confirm that for categories (e.g in menu) also have a sort order by position ASC ?

@ghost ghost assigned mslabko Jul 28, 2020
dnyomo added 2 commits July 30, 2020 15:17
…CriteriaBuilder.php

Remove the _id from the order that is being passed to the product collection in ProductSearch.php
Updated the position to be DESC passed to the product collection in ProductSearch.php
Updated the ProductSearchTest.php to have the items in the attended order
…CriteriaBuilder.php

Remove the _id from the order that is being passed to the product collection in ProductSearch.php
Updated the position to be DESC passed to the product collection in ProductSearch.php
Updated the ProductSearchTest.php to have the items in the attended order
@Dnyomo
Copy link
Author

Dnyomo commented Jul 30, 2020

@magento run WebAPI Tests, Integration Tests

{
$ordersArray = [];
$sortOrders = $searchCriteria->getSortOrders();
if (is_array($sortOrders)) {
foreach ($sortOrders as $sortOrder) {
$ordersArray[$sortOrder->getField()] = $sortOrder->getDirection();
if ($sortOrder->getField() !== '_id') {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this check? It will ignore any sort order defined by user in GQL query

@@ -104,6 +104,7 @@ public function build(array $args, bool $includeAggregation): SearchCriteriaInte
$this->addDefaultSortOrder($searchCriteria, $isSearch);
}

$this->addEntityIdSort($searchCriteria, $isSearch);
Copy link
Member

Choose a reason for hiding this comment

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

could you please verify the behavior for GQL query which will filter by category id/ids, e.g.

{
  products(filter: {category_id: {in: ["3", "5"]}}) {
   items {    
      sku
    }
  }
}
 
{
  products(filter: {category_id: {eq:"3"}}) {
   items {    
      sku
    }
  }
}

In these examples we should add order by "position ASC" only for the second case.
And only if the sort was provided explicitly in GQL query it should be added as is.

I'm afraid that current implementation in addDefaultSortOrder method don't take this into account

I think we dont' have specific tests for that and it make sense to add this for GQL coverage
(@yaroslavGoncharuk can you help us with steps?)

Copy link
Author

Choose a reason for hiding this comment

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

I did test this and because this method (\Magento\Catalog\Model\ResourceModel\Product\Collection::addAttributeToSort line 1749) converts the attribute position to entity_id. I think it makes sense to always have a position set as a sort.

@Dnyomo
Copy link
Author

Dnyomo commented Jul 31, 2020

@magento run WebAPI Tests

@Dnyomo
Copy link
Author

Dnyomo commented Jul 31, 2020

@magento run WebAPI Tests

@Dnyomo
Copy link
Author

Dnyomo commented Jul 31, 2020

@magento run WebAPI Tests

@yaroslavGoncharuk
Copy link
Contributor

yaroslavGoncharuk commented Aug 1, 2020

@Dnyomo Query with condition eq returns "Internal server error"

{
  products(filter: {category_id: {eq:"3"}}) {
   items {    
      sku
    }
  }
} 

Works well if eq is replaced by in OR sort: {price: ASC} argument is added

…array.

Reverted the tests back to check on products
@Dnyomo
Copy link
Author

Dnyomo commented Aug 2, 2020

@magento run WebAPI Tests

…array.

Reverted the tests back to check on products
@Dnyomo
Copy link
Author

Dnyomo commented Aug 2, 2020

@magento run WebAPI Tests

@mslabko
Copy link
Member

mslabko commented Aug 3, 2020

@magento run all tests

@mslabko
Copy link
Member

mslabko commented Aug 3, 2020

@mslabko
Copy link
Member

mslabko commented Aug 3, 2020

@magento run Functional Tests CE,Functional Tests B2B

@mslabko
Copy link
Member

mslabko commented Aug 4, 2020

@magento run Functional Tests B2B

dnyomo added 2 commits August 4, 2020 13:15
…m:Dnyomo/magento2 into storefront-issue_104_default_sort_order_b
@Dnyomo
Copy link
Author

Dnyomo commented Aug 4, 2020

@magento run Static Tests

@Dnyomo
Copy link
Author

Dnyomo commented Aug 4, 2020

@magento run Static Tests

@Dnyomo
Copy link
Author

Dnyomo commented Aug 4, 2020

@magento run Static Tests

@Dnyomo
Copy link
Author

Dnyomo commented Aug 4, 2020

@magento run Static Tests

@Dnyomo
Copy link
Author

Dnyomo commented Aug 4, 2020

@magento run Static Tests

@mslabko
Copy link
Member

mslabko commented Aug 4, 2020

@magento run all tests

@mslabko
Copy link
Member

mslabko commented Aug 5, 2020

@magento run Functional Tests B2B

@magento-engcom-team magento-engcom-team merged commit 351a90c into magento:2.4-develop Aug 6, 2020
@magento magento deleted a comment from m2-assistant bot Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default sort order
4 participants