AbandonedCart
1 year ago
Orders
3 months ago
BuysAProductTrigger.php
2 years ago
BuysFromACategoryTrigger.php
2 years ago
BuysFromATagTrigger.php
1 year ago
index.php
3 years ago
BuysAProductTrigger.php
159 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\WooCommerce\Triggers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Control\FilterHandler; |
| 9 | use MailPoet\Automation\Engine\Data\Field; |
| 10 | use MailPoet\Automation\Engine\Data\Filter; |
| 11 | use MailPoet\Automation\Engine\Data\FilterGroup; |
| 12 | use MailPoet\Automation\Engine\Data\StepRunArgs; |
| 13 | use MailPoet\Automation\Engine\Data\StepValidationArgs; |
| 14 | use MailPoet\Automation\Engine\Data\Subject; |
| 15 | use MailPoet\Automation\Engine\Hooks; |
| 16 | use MailPoet\Automation\Engine\Integration\Trigger; |
| 17 | use MailPoet\Automation\Engine\Storage\AutomationRunStorage; |
| 18 | use MailPoet\Automation\Engine\WordPress; |
| 19 | use MailPoet\Automation\Integrations\Core\Filters\EnumArrayFilter; |
| 20 | use MailPoet\Automation\Integrations\Core\Filters\EnumFilter; |
| 21 | use MailPoet\Automation\Integrations\WooCommerce\Subjects\CustomerSubject; |
| 22 | use MailPoet\Automation\Integrations\WooCommerce\Subjects\OrderStatusChangeSubject; |
| 23 | use MailPoet\Automation\Integrations\WooCommerce\Subjects\OrderSubject; |
| 24 | use MailPoet\Validator\Builder; |
| 25 | use MailPoet\Validator\Schema\ObjectSchema; |
| 26 | use MailPoet\WooCommerce\Helper as WooCommerceHelper; |
| 27 | |
| 28 | class BuysAProductTrigger implements Trigger { |
| 29 | public const KEY = 'woocommerce:buys-a-product'; |
| 30 | |
| 31 | /** @var WordPress */ |
| 32 | private $wp; |
| 33 | |
| 34 | /** @var WooCommerceHelper */ |
| 35 | private $wc; |
| 36 | |
| 37 | /** @var AutomationRunStorage */ |
| 38 | private $automationRunStorage; |
| 39 | |
| 40 | /** @var FilterHandler */ |
| 41 | private $filterHandler; |
| 42 | |
| 43 | public function __construct( |
| 44 | WordPress $wp, |
| 45 | WooCommerceHelper $wc, |
| 46 | AutomationRunStorage $automationRunStorage, |
| 47 | FilterHandler $filterHandler |
| 48 | ) { |
| 49 | $this->wp = $wp; |
| 50 | $this->wc = $wc; |
| 51 | $this->automationRunStorage = $automationRunStorage; |
| 52 | $this->filterHandler = $filterHandler; |
| 53 | } |
| 54 | |
| 55 | public function getKey(): string { |
| 56 | return self::KEY; |
| 57 | } |
| 58 | |
| 59 | public function getName(): string { |
| 60 | // translators: automation trigger title |
| 61 | return __('Customer buys a product', 'mailpoet'); |
| 62 | } |
| 63 | |
| 64 | public function getArgsSchema(): ObjectSchema { |
| 65 | return Builder::object([ |
| 66 | 'product_ids' => Builder::array( |
| 67 | Builder::integer() |
| 68 | )->minItems(1)->required(), |
| 69 | 'to' => Builder::string()->required()->default('wc-completed'), |
| 70 | ]); |
| 71 | } |
| 72 | |
| 73 | public function getSubjectKeys(): array { |
| 74 | return [ |
| 75 | OrderSubject::KEY, |
| 76 | OrderStatusChangeSubject::KEY, |
| 77 | CustomerSubject::KEY, |
| 78 | ]; |
| 79 | } |
| 80 | |
| 81 | public function validate(StepValidationArgs $args): void { |
| 82 | } |
| 83 | |
| 84 | public function registerHooks(): void { |
| 85 | $this->wp->addAction( |
| 86 | 'woocommerce_order_status_changed', |
| 87 | [ |
| 88 | $this, |
| 89 | 'handle', |
| 90 | ], |
| 91 | 10, |
| 92 | 3 |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param int $orderId |
| 98 | * @param string $from |
| 99 | * @param string $to |
| 100 | * @return void |
| 101 | */ |
| 102 | public function handle($orderId, $from, $to): void { |
| 103 | $order = $this->wc->wcGetOrder($orderId); |
| 104 | if (!$order) { |
| 105 | return; |
| 106 | } |
| 107 | $this->wp->doAction(Hooks::TRIGGER, $this, [ |
| 108 | new Subject(OrderSubject::KEY, ['order_id' => $orderId]), |
| 109 | new Subject(CustomerSubject::KEY, ['customer_id' => $order->get_customer_id(), 'order_id' => $orderId]), |
| 110 | new Subject(OrderStatusChangeSubject::KEY, ['from' => $from, 'to' => $to]), |
| 111 | ]); |
| 112 | } |
| 113 | |
| 114 | public function isTriggeredBy(StepRunArgs $args): bool { |
| 115 | |
| 116 | //Trigger the run only once. |
| 117 | $orderSubjectData = $args->getSingleSubjectEntryByClass(OrderSubject::class)->getSubjectData(); |
| 118 | if ($this->automationRunStorage->getCountByAutomationAndSubject($args->getAutomation(), $orderSubjectData) > 0) { |
| 119 | return false; |
| 120 | } |
| 121 | $group = new FilterGroup( |
| 122 | '', |
| 123 | FilterGroup::OPERATOR_AND, |
| 124 | $this->getFilters($args) |
| 125 | ); |
| 126 | return $this->filterHandler->matchesGroup($group, $args); |
| 127 | } |
| 128 | |
| 129 | protected function getFilters(StepRunArgs $args): array { |
| 130 | $triggerArgs = $args->getStep()->getArgs(); |
| 131 | $filters = [ |
| 132 | Filter::fromArray([ |
| 133 | 'id' => '', |
| 134 | 'field_type' => Field::TYPE_ENUM_ARRAY, |
| 135 | 'field_key' => 'woocommerce:order:products', |
| 136 | 'condition' => EnumArrayFilter::CONDITION_MATCHES_ANY_OF, |
| 137 | 'args' => [ |
| 138 | 'value' => $triggerArgs['product_ids'] ?? [], |
| 139 | ], |
| 140 | ]), |
| 141 | ]; |
| 142 | $status = str_replace('wc-', '', $triggerArgs['to'] ?? 'completed'); |
| 143 | if ($status === 'any') { |
| 144 | return $filters; |
| 145 | } |
| 146 | |
| 147 | $filters[] = Filter::fromArray([ |
| 148 | 'id' => '', |
| 149 | 'field_type' => Field::TYPE_ENUM, |
| 150 | 'field_key' => 'woocommerce:order:status', |
| 151 | 'condition' => EnumFilter::IS_ANY_OF, |
| 152 | 'args' => [ |
| 153 | 'value' => [$status], |
| 154 | ], |
| 155 | ]); |
| 156 | return $filters; |
| 157 | } |
| 158 | } |
| 159 |