Skip to content

Commit

Permalink
Added product condition rule for variant conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Dec 11, 2024
1 parent 71b377a commit 225c924
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Release Notes for Craft Commerce (WIP)

## Administration
### Store Management

- Added support for `to`, `bcc`, and `cc` email fields to support environment variables. ([#3738](https://github.com/craftcms/commerce/issues/3738))
- Added an `originalCart` value to the `commerce/update-cart` failed ajax response. ([#430](https://github.com/craftcms/commerce/issues/430))
- Added a new "Payment Gateway" order condition rule. ([#3722](https://github.com/craftcms/commerce/discussions/3722))
- Order conditions can now have a “Payment Gateway” rule. ([#3722](https://github.com/craftcms/commerce/discussions/3722))
- Variant conditions can now have a “Product” rule.

### Extensibility

- Added `craft\commerce\elements\conditions\variants\ProductConditionRule`.
68 changes: 68 additions & 0 deletions src/elements/conditions/variants/ProductConditionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\commerce\elements\conditions\variants;

use Craft;
use craft\base\conditions\BaseElementSelectConditionRule;
use craft\base\ElementInterface;
use craft\commerce\elements\db\VariantQuery;
use craft\commerce\elements\Product;
use craft\commerce\elements\Variant;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\ElementQueryInterface;

/**
* Products Condition Rule
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 5.3.0
*/
class ProductConditionRule extends BaseElementSelectConditionRule implements ElementConditionRuleInterface
{
/**
* @inheritdoc
*/
protected function elementType(): string
{
return Product::class;
}

/**
* @inheritdoc
*/
public function getLabel(): string
{
return Craft::t('commerce', 'Product');
}

/**
* @inheritdoc
*/
public function getExclusiveQueryParams(): array
{
return ['product', 'productId', 'primaryOwnerId', 'primaryOwner', 'owner', 'ownerId'];
}

/**
* @inheritdoc
*/
public function modifyQuery(ElementQueryInterface $query): void
{
/** @var VariantQuery $query */
$query->ownerId($this->getElementId());
}

/**
* @inheritdoc
*/
public function matchElement(ElementInterface $element): bool
{
/** @var Variant $element */
return $element->getOwnerId() == $this->getElementId();
}
}
1 change: 1 addition & 0 deletions src/elements/conditions/variants/VariantCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class VariantCondition extends ElementCondition
protected function selectableConditionRules(): array
{
return array_merge(parent::selectableConditionRules(), [
ProductConditionRule::class,
SkuConditionRule::class,
]);
}
Expand Down

0 comments on commit 225c924

Please sign in to comment.