Skip to content

[GraphQl] 29168 TierPrices filtering #29675

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

Conversation

Usik2203
Copy link
Contributor

Description (*)

This PR fixes issue with TierPrices filtering
Screenshot 2020-08-19 at 14 31 29

Result Before
We see all prices for each quantity
Screenshot 2020-08-19 at 14 47 35

Result After
We see lower price for each quantity
Screenshot 2020-08-19 at 14 47 57

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes Tier prices filtering in GraphQl - prices for different customer group but same quantity are not filtered out #29168>

Manual testing scenarios (*)

  1. ...
  2. ...

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 Aug 19, 2020

Hi @Usik2203. 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

@magento-engcom-team magento-engcom-team added Component: CatalogCustomerGraphQl Release Line: 2.4 Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner labels Aug 19, 2020
@Usik2203
Copy link
Contributor Author

@magento run all tests

@rogyar rogyar self-assigned this Aug 19, 2020
Copy link
Contributor

@rogyar rogyar left a comment

Choose a reason for hiding this comment

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

Hi @Usik2203. Please, check my recommendations below.
Also, do you have covering this case by API-functional test in your plans?

*
* @return bool
*/
private function isFirstPriceBetter(float $firstPrice, float $secondPrice): bool
Copy link
Contributor

Choose a reason for hiding this comment

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

I would not suggest creating a separate method for such a simple comparison. It makes code harder to read and introduces semantical complexity. In the future, if we will need more complex comparison we can always create a separate method. But on this stage, I would keep things simple

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

private function filterTierPrices(array $tierPrices): array
{
$qtyCache = [];
foreach ($tierPrices as $item => &$price) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We are passing the &$price variable by reference but it does not seem like we modify it somehow in the loop below. But I may miss something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Copy link
Contributor

Choose a reason for hiding this comment

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

@Usik2203
Both methods, filterTierPrices and formatProductTierPrices, which are called one after another, are using foreach to loop through the tier prices.

Given that this code executes when a query searches for products, please consider a situation where we might have large b2b catalog with plenty tiers for different customer groups and a relatively short search phrase, yielding plenty of results.
Having to loop twice through all tier prices in all search results, might lead to performance degradation.

I am wondering, could these two methods be somehow combined, so that we get the same results using foreach only once?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pmarjan Done

Copy link
Contributor

Choose a reason for hiding this comment

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

@Usik2203
From "clarity of intent" perspective, when one reads through the "resolve" function alone, it seems as though formatProductTierPrices function does only what its name says. In fact this function's comment seems to support that.
But, that is misleading.

So, I am wondering can we have a function named like filterAndFormatProductTierPrices, that invokes a foreach loop, and inside this loop, one below the other filterTierPrices and formatProductTierPrices are called?

Check for example, when an unfortunate situation like we have, calls for two different things to be done in the same method:
League\Flysystem\Filesystem::readAndDelete()
internally first calls
$this->read()
and later
$this->delete()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pmarjan Done

@Usik2203
Copy link
Contributor Author

@magento run all tests

@Usik2203
Copy link
Contributor Author

Hi @rogyar
I have applied your code review recommendations and added API test.
Thanks

@pmarjan pmarjan self-requested a review August 21, 2020 11:34
@Usik2203
Copy link
Contributor Author

@magento run all tests

@Usik2203
Copy link
Contributor Author

@magento run all tests

@Usik2203
Copy link
Contributor Author

@magento run all tests

@rogyar rogyar added the Auto-Tests: Covered All changes in Pull Request is covered by auto-tests label Aug 29, 2020
@magento-engcom-team
Copy link
Contributor

Hi @rogyar, thank you for the review.
ENGCOM-8099 has been created to process this Pull Request

@nrkapoor nrkapoor added this to the 2.4.2 milestone Oct 29, 2020
@nrkapoor nrkapoor requested review from pmarjan, mauragcyrus and dthampy and removed request for pmarjan October 29, 2020 08:45
@@ -120,42 +120,89 @@ function () use ($productId, $context) {

Copy link
Contributor

Choose a reason for hiding this comment

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

can you fix types on $this->tiers->addProductFilter($productId) ? \Magento\CatalogCustomerGraphQl\Model\Resolver\Product\Price\Tiers::addProductFilter has no strict types
public function addProductFilter($productId): void
would be
public function addProductFilter(int $productId): void

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

* @param ProductTierPriceInterface $tierPrice
* @param array $tiers
*/
private function formatTierPrices(float $productPrice, StoreInterface $store, &$tierPrice, &$tiers)
Copy link
Contributor

Choose a reason for hiding this comment

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

we kind of prohibit passing params by reference &$tierPrice, &$tiers
You better want a function that returns something. You rarely want a function that returns 2 things.
formatTierPrices can return $tiers for example.
Objects are passed by reference by default so you don't need &$tierPrice to do $tierPrice->setValue
You can also do that outside formatTierPrices and don't set anything in tierPrice, just get
Plus you only need getValue and getQty and getPercentageValue from it. You can use those params directly not by passing the whole tierPrice.
From store you only need CurrencyCode not store.
Suddenly your method becomes: formatTierPricesValues(productPrice, tierPriceValue, TierPricePercentage, tierPriceQty): array (where array is $tiers)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

array $tierPrices,
int $key,
ProductTierPriceInterface $tierPriceItem,
array &$qtyCache,
Copy link
Contributor

Choose a reason for hiding this comment

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

$qtyCache doesn't need to be passed by reference
it can be part of your object \Magento\CatalogCustomerGraphQl\Model\Resolver\PriceTiers
by declaring a private $productTiersByQuanity[] //you can dcument that this acts like a cache
it can hold prices like $productTiersByQuanity[customerGroup][productId][qty] so it can be used for multiple products and if called multiple times for the same product, you can just fetch it and not even need to recalculate it.
not sure if customerGroup is required. probably not. you'l only get the relevant tiers for current customerGroup
of course filterTierPrices : array which is the filtered tiers, and you can pass formatted tiers from previous function return

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@Usik2203
Copy link
Contributor Author

@magento run all tests

@o-iegorov
Copy link
Contributor

@magento run Functional Tests CE, Integration Tests

@o-iegorov
Copy link
Contributor

@magento run Functional Tests EE

@o-iegorov
Copy link
Contributor

@magento run Functional Tests EE

@m2-assistant
Copy link

m2-assistant bot commented Nov 13, 2020

Hi @Usik2203, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Award: test coverage Component: CatalogCustomerGraphQl Partner: Atwix Pull Request is created by partner Atwix partners-contribution Pull Request is created by Magento Partner Progress: ready for testing Project: GraphQL Release Line: 2.4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tier prices filtering in GraphQl - prices for different customer group but same quantity are not filtered out
8 participants