From 9ecd55bf5158898d8a07d56da1ab9b665aec3095 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Wed, 3 Jul 2024 09:36:37 +0100 Subject: [PATCH 1/2] WIP variant query fix for `hasSales` --- src/elements/db/VariantQuery.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/elements/db/VariantQuery.php b/src/elements/db/VariantQuery.php index 0eae719050..66785ad156 100644 --- a/src/elements/db/VariantQuery.php +++ b/src/elements/db/VariantQuery.php @@ -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)); From 17e3c751a932a92f80916312d383f94568a31534 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Wed, 3 Jul 2024 09:51:43 +0100 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5021dc1481..cd58ca53e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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))