Fixed query issues and added ability to pre-filter purchase patterns#3
Merged
Tam merged 1 commit intoethercreative:v1-devfrom Dec 17, 2019
jmauzyk:v1
Merged
Fixed query issues and added ability to pre-filter purchase patterns#3Tam merged 1 commit intoethercreative:v1-devfrom jmauzyk:v1
Tam merged 1 commit intoethercreative:v1-devfrom
jmauzyk:v1
Conversation
Member
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes proposed in this pull request:
Change
ProductQueryExtendedjoinCurrently, using
innerJoin()only returns products that have a record in thepurchase_countstable. As a result, it could render thepaddingQueryuseless, especially in cases such as new shops where a majority of products may not have any purchase records. UsingleftJoin()resolves this issue.Exclude purchase pattern IDs from
paddingQueryCurrently product IDs queried from the
purchase_patternstable are not excluded from thepaddingQuery. This can result in returning a query with fewer products that the designatedlimitwhen a duplicate ID is returned by thepaddingQuery. The_mergeIds()method examines any existingidproperty of thepaddingQueryand excludes any of the product IDs found inpurchase_patternsbefore executing the query.Add ability to filter IDs queried from
purchase_patternsThis was something I tried to resolve in a previous pull request but was not merged for the reasons outlined in this comment. While I initially thought I could resolve the issue using method suggested in the comment, I found that there are scenarios (especially when using a
limit) where it does not work as desired.{% set customersAlsoBought = craft.purchasePatterns.related( product, 3, craft.products.relatedTo(product.myCategory).availableForPurchase(1) ).availableForPurchase(1).all() %}Example 1:
The
related()above returns three products, but they all haveavailableForPurchaseset to false. They are subsequently filtered by.availableForPurchase(1), returning an empty array.Example 2:
The
related()above retrieves 1 product frompurchase_patterns, filling in the remaining two products with thepaddingQuery. The two products haveavailableForPurchaseset to true, but the product frompurchase_patternsdoes not. The query is subsequently filtered by.availableForPurchase(1), returning an array of two products instead of three.I added a
filtersparameter and deferred limiting until a second query that applies the filters to product Ids frompurchase_patterns. Doing so allows these relationships to be queried with criteria while still ensuring results that consistently match the limit. The query above would be revised to:{% set customersAlsoBought = craft.purchasePatterns.related( product, 3, craft.products.relatedTo(product.myCategory).availableForPurchase(1), { availableForPurchase: 1 } ).all() %}This could also be useful in other situations, such as only retrieving purchase patterns for certain product types (e.g. excluding replacement parts so only retail products are returned).
Thanks for your consideration!