Skip to content

Commit

Permalink
Fix bug introduced by new user condition
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRonbot committed Aug 26, 2022
1 parent eaf5680 commit 818d3b4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/behaviors/ProductBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
}
11 changes: 11 additions & 0 deletions src/elements/ProductLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/services/ProductLabels.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
}
Expand Down

0 comments on commit 818d3b4

Please sign in to comment.