Skip to content

Fix missing discountable_type checks in Discount scope methods#2250

Closed
fredmarmillod wants to merge 10 commits intolunarphp:1.xfrom
fredmarmillod:fix/discount-scope-discountable-type-checks
Closed

Fix missing discountable_type checks in Discount scope methods#2250
fredmarmillod wants to merge 10 commits intolunarphp:1.xfrom
fredmarmillod:fix/discount-scope-discountable-type-checks

Conversation

@fredmarmillod
Copy link
Contributor

@fredmarmillod fredmarmillod commented Jul 21, 2025

Problem

The scopeProducts, scopeProductVariants, and scopeCollections methods in the Discount model were missing discountable_type checks in their whereDoesntHave('discountables') subqueries. This could lead to incorrect filtering when discounts have mixed discountable types.

For example, if a discount had Product discountables but a query was looking for Collection-related discounts, the scope might incorrectly include or exclude that discount because it wasn't properly filtering by the specific discountable type.

Solution

Added whereDiscountableType() checks to the whereDoesntHave('discountables') subqueries in all three scope methods:
scopeCollections: Now properly filters for Collection::morphName() discountable types
scopeProducts: Now properly filters for Product::morphName() discountable types
scopeProductVariants: Now properly filters for ProductVariant::morphName() discountable types

Changes

packages/core/src/Models/Discount.php:

  • Fixed scopeCollections() method to include discountable type check
  • Fixed scopeProducts() method to include discountable type check
  • Fixed scopeProductVariants() method to include discountable type check

tests/core/Unit/Models/DiscountTest.php:

  • Added comprehensive test suite for scopeCollections()
  • Added comprehensive test suite for scopeProducts()
  • Added comprehensive test suite for scopeProductVariants()

fredmarmillod and others added 2 commits July 21, 2025 09:13
- Added whereDiscountableType() checks to whereDoesntHave subqueries in scopeCollections, scopeProducts, and scopeProductVariants
- Ensures proper filtering when discounts have mixed discountable types
- Added comprehensive test coverage for all three scope methods
- Tests verify correct behavior with mixed types and edge cases
@vercel vercel bot requested a deployment to Preview July 21, 2025 07:19 Abandoned
coderabbitai[bot]

This comment was marked as outdated.

- Add discountable_type filtering to products() and productVariants() scopes
- Ensures scopes only match discountables of the correct type
- Add comprehensive tests covering the new type filtering behavior
Keep local implementation of collections scope tests using pivot table relationships
@fredmarmillod
Copy link
Contributor Author

This pull request also fixes the scopeCollections. It was previously using the discountables relation instead of using the lunar_collection_discounttable.

I also added a scopeBrands that works the same way as scopeCollections

@glennjacobs
Copy link
Contributor

@ryanmitchell could I have your input on this one?

@ryanmitchell
Copy link
Contributor

I understand the issue, but I don't understand why some much of the code has been changed, for example using the prefix. Is there a reason why this PR has gone that far?

For me it should just be a case of adding ->whereDiscounType() on the whereDoesntHave() subqueries.

@fredmarmillod
Copy link
Contributor Author

fredmarmillod commented Aug 14, 2025

I started by just adding whereDiscountType on the whereDoesntHave and tested filtering with discounts having multiple limitations of different types but it triggered some mysql ambiguous column names error. That's why I added prefix. Then I noticed thaat brands and collection limitations are using collection_discount and brand_discount tables instead of the discountables table so I updated collection scope to use collection_discount and created a brand scope.

@ryanmitchell
Copy link
Contributor

hmm, then some of my work on #2214 was incorrect. I had assumed (seemingly incorrectly) that everything was stored in the discountables table.

The options are either:

  1. maintain the existing table structure but then ProductConditionRelationManager needs to be updated to put collections into collection_discount instead.

  2. drop the collection_discount, brand_discount tables and move them to be morph relationships on discountables

To me (2) makes more sense as its already a morph table.

@ryanmitchell
Copy link
Contributor

@glennjacobs what approach would you prefer?
@fredmarmillod let me know if you need me to work on these changes

@fredmarmillod
Copy link
Contributor Author

@ryanmitchell Yes, that would be great, thanks

@lunarphp lunarphp deleted a comment from coderabbitai bot Nov 11, 2025
@github-project-automation github-project-automation bot moved this to Todo in Roadmap Nov 11, 2025
@lunarphp lunarphp deleted a comment from vercel bot Nov 11, 2025
@glennjacobs
Copy link
Contributor

@glennjacobs what approach would you prefer? @fredmarmillod let me know if you need me to work on these changes

I think we stick with the current tables, option 1.

@ryanmitchell
Copy link
Contributor

I've opened a PR building on this #2345 - so this one can be closed.

@glennjacobs
Copy link
Contributor

Closing in preference of #2345

@github-project-automation github-project-automation bot moved this from Todo to Done in Roadmap Nov 12, 2025
glennjacobs added a commit that referenced this pull request Dec 8, 2025
This PR builds on the work done in
#2250

It:
1. Adds a brand check 
2. Seperates collections into its own relation manager to allow it to
continue to use collection_discount table

It's breaking, but for v2 we should consolidate everything into the
discountable table - I missed these when I did this initial piece of
work. It would make the UI much more streamlined and from a data point
of view reduce the redundancy of the tables.

---------

Co-authored-by: fredmarmillod <[email protected]>
Co-authored-by: Author <[email protected]>
Co-authored-by: Frédéric Marmillod <[email protected]>
Co-authored-by: Glenn Jacobs <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants