diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d0f30c..6a43021 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +## 4.0.6 - 2022-08-26 +### Fixed +- Fix bug introduced by new user condition addition + ## 4.0.5 - 2022-08-26 ### Added - Add user condition for matching by user attributes diff --git a/composer.json b/composer.json index 6890ec5..9225986 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "thepixelage/craft-productlabels", "description": "Product Labels is a Craft Commerce plugin for creating labels for products", "type": "craft-plugin", - "version": "4.0.5", + "version": "4.0.6", "keywords": [ "craft", "cms", diff --git a/src/behaviors/ProductBehavior.php b/src/behaviors/ProductBehavior.php index 1c95e35..533b609 100644 --- a/src/behaviors/ProductBehavior.php +++ b/src/behaviors/ProductBehavior.php @@ -16,8 +16,8 @@ public function getProductLabels(): array $productLabels = Plugin::getInstance()->productLabels->getAllProductLabels(); return array_filter($productLabels, function (ProductLabel $productLabel) use ($product) { - return in_array($product->id, $productLabel->getMatchedProductIds()) && - $productLabel->getMatchCurrentUser(); + return ($productLabel->getMatchAllProducts() || in_array($product->id, $productLabel->getMatchedProductIds())) + && $productLabel->getMatchCurrentUser(); }); } } diff --git a/src/elements/ProductLabel.php b/src/elements/ProductLabel.php index 933f65d..8526045 100644 --- a/src/elements/ProductLabel.php +++ b/src/elements/ProductLabel.php @@ -39,6 +39,7 @@ class ProductLabel extends Element private ElementConditionInterface|null $_productCondition = null; private ElementConditionInterface|null $_userCondition = null; private array $matchedProductIds = []; + private bool $matchAllProducts = false; private bool $matchCurrentUser = false; public function __construct($config = []) @@ -138,6 +139,16 @@ public function setMatchedProductIds($ids) $this->matchedProductIds = $ids; } + public function getMatchAllProducts(): bool + { + return $this->matchAllProducts; + } + + public function setMatchAllProducts($match) + { + $this->matchAllProducts = $match; + } + public function getMatchCurrentUser(): bool { return $this->matchCurrentUser; diff --git a/src/services/ProductLabels.php b/src/services/ProductLabels.php index 75ceddc..0a17e60 100644 --- a/src/services/ProductLabels.php +++ b/src/services/ProductLabels.php @@ -187,16 +187,16 @@ private function _productLabels(): MemoizableArray foreach ($productLabels as $productLabel) { $productCondition = $productLabel->getProductCondition(); if (count($productCondition->getConditionRules()) > 0) { - foreach ($productCondition->getConditionRules() as $rule) { - $query = Product::find(); - $productCondition->modifyQuery($query); - $productLabel->setMatchedProductIds($query->ids()); - } + $query = Product::find(); + $productCondition->modifyQuery($query); + $productLabel->setMatchedProductIds($query->ids()); + } else { + $productLabel->setMatchAllProducts(true); } $userCondition = $productLabel->getUserCondition(); $productLabel->setMatchCurrentUser( - $userCondition->conditionRules == 0 || + count($userCondition->conditionRules) == 0 || ($currentUser && $userCondition->matchElement($currentUser)) ); }