Skip to content

Commit

Permalink
Merge pull request #3568 from craftcms/feature/pt-1885-3x-hassales-at…
Browse files Browse the repository at this point in the history
…tribute-not-filtering-out-items-that-are-not

[3.x] Variant query fix for `hasSales`
  • Loading branch information
nfourtythree authored Jul 3, 2024
2 parents 5c150b0 + 17e3c75 commit e20c03f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Craft Commerce

## Unreleased

- Fixed a bug where non-promotable variants could be returned when querying using `hasSales()`. ([#3553](https://github.com/craftcms/commerce/issues/3553))

## 3.4.23.1 - 2024-04-15

- Fixed an error that could occur when rendering a PDF. ([#2633](https://github.com/craftcms/commerce/issues/2633))
Expand Down
21 changes: 21 additions & 0 deletions src/elements/db/VariantQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,27 @@ protected function beforePrepare(): bool

$categoryRestrictedVariantIds = array_merge($sourceVariantIds, $targetVariantIds);
$categoryRestrictedProductIds = array_merge($sourceProductIds, $targetProductIds);

// Remove non-promotable products
$categoryRestrictedProductIds = (new Query())
->select('id')
->from(Table::PRODUCTS)
->where([
'id' => $categoryRestrictedProductIds,
'promotable' => true,
])
->column();

// Remove variants that are related to non-promotable products
$categoryRestrictedVariantIds = (new Query())
->select('v.id')
->from(Table::VARIANTS . ' v')
->leftJoin(Table::PRODUCTS . ' p', '[[p.id]] = [[v.productId]]')
->where([
'id' => $categoryRestrictedVariantIds,
'p.promotable' => true,
])
->column();
}

$variantIds = array_unique(array_merge($purchasableRestrictedIds, $categoryRestrictedVariantIds));
Expand Down

0 comments on commit e20c03f

Please sign in to comment.