Transformers
1 year ago
BaseLocationCountryRuleProcessor.php
2 years ago
BaseLocationStateRuleProcessor.php
2 years ago
ComparisonOperation.php
2 years ago
ContextPluginsRuleProcessor.php
1 year ago
EvaluateAndGetStatus.php
2 years ago
EvaluateOverrides.php
1 year ago
EvaluationLogger.php
1 year ago
FailRuleProcessor.php
2 years ago
GetRuleProcessor.php
2 years ago
GetRuleProcessorForContext.php
1 year ago
IsEcommerceRuleProcessor.php
2 years ago
IsWooExpressRuleProcessor.php
2 years ago
NotRuleProcessor.php
2 years ago
NoteStatusRuleProcessor.php
2 years ago
OnboardingProfileRuleProcessor.php
2 years ago
OptionRuleProcessor.php
2 years ago
OrRuleProcessor.php
2 years ago
OrderCountRuleProcessor.php
2 years ago
OrdersProvider.php
2 years ago
PassRuleProcessor.php
2 years ago
PluginVersionRuleProcessor.php
2 years ago
PluginsActivatedRuleProcessor.php
2 years ago
ProductCountRuleProcessor.php
1 year ago
PublishAfterTimeRuleProcessor.php
2 years ago
PublishBeforeTimeRuleProcessor.php
2 years ago
RuleEvaluator.php
1 year ago
RuleProcessorInterface.php
2 years ago
StoredStateRuleProcessor.php
2 years ago
StoredStateSetupForProducts.php
2 years ago
TotalPaymentsVolumeProcessor.php
1 year ago
WCAdminActiveForProvider.php
2 years ago
WCAdminActiveForRuleProcessor.php
2 years ago
WooCommerceAdminUpdatedRuleProcessor.php
2 years ago
ProductCountRuleProcessor.php
81 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Rule processor that performs a comparison operation against the number of |
| 4 | * products. |
| 5 | */ |
| 6 | |
| 7 | namespace Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors; |
| 8 | |
| 9 | use Automattic\WooCommerce\Enums\ProductStatus; |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * Rule processor that performs a comparison operation against the number of |
| 15 | * products. |
| 16 | */ |
| 17 | class ProductCountRuleProcessor implements RuleProcessorInterface { |
| 18 | |
| 19 | /** |
| 20 | * The product query. |
| 21 | * |
| 22 | * @var WC_Product_Query |
| 23 | */ |
| 24 | protected $product_query; |
| 25 | |
| 26 | /** |
| 27 | * Constructor. |
| 28 | * |
| 29 | * @param object $product_query The product query. |
| 30 | */ |
| 31 | public function __construct( $product_query = null ) { |
| 32 | $this->product_query = null === $product_query |
| 33 | ? new \WC_Product_Query( |
| 34 | array( |
| 35 | 'limit' => 1, |
| 36 | 'paginate' => true, |
| 37 | 'return' => 'ids', |
| 38 | 'status' => array( ProductStatus::PUBLISH ), |
| 39 | ) |
| 40 | ) |
| 41 | : $product_query; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Performs a comparison operation against the number of products. |
| 46 | * |
| 47 | * @param object $rule The specific rule being processed by this rule processor. |
| 48 | * @param object $stored_state Stored state. |
| 49 | * |
| 50 | * @return bool The result of the operation. |
| 51 | */ |
| 52 | public function process( $rule, $stored_state ) { |
| 53 | $products = $this->product_query->get_products(); |
| 54 | |
| 55 | return ComparisonOperation::compare( |
| 56 | $products->total, |
| 57 | $rule->value, |
| 58 | $rule->operation |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Validates the rule. |
| 64 | * |
| 65 | * @param object $rule The rule to validate. |
| 66 | * |
| 67 | * @return bool Pass/fail. |
| 68 | */ |
| 69 | public function validate( $rule ) { |
| 70 | if ( ! isset( $rule->value ) ) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if ( ! isset( $rule->operation ) ) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | } |
| 81 |