mailpoet
/
lib
/
Automation
/
Integrations
/
WooCommerce
/
Triggers
/
Orders
/
OrderStatusChangedTrigger.php
OrderCancelledTrigger.php
2 years ago
OrderCompletedTrigger.php
2 years ago
OrderCreatedTrigger.php
3 months ago
OrderNoteAddedTrigger.php
1 year ago
OrderStatusChangedTrigger.php
2 years ago
index.php
2 years ago
OrderStatusChangedTrigger.php
105 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\WooCommerce\Triggers\Orders; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\StepRunArgs; |
| 9 | use MailPoet\Automation\Engine\Data\StepValidationArgs; |
| 10 | use MailPoet\Automation\Engine\Data\Subject; |
| 11 | use MailPoet\Automation\Engine\Hooks; |
| 12 | use MailPoet\Automation\Engine\Integration\Trigger; |
| 13 | use MailPoet\Automation\Engine\WordPress; |
| 14 | use MailPoet\Automation\Integrations\WooCommerce\Payloads\OrderStatusChangePayload; |
| 15 | use MailPoet\Automation\Integrations\WooCommerce\Subjects\CustomerSubject; |
| 16 | use MailPoet\Automation\Integrations\WooCommerce\Subjects\OrderStatusChangeSubject; |
| 17 | use MailPoet\Automation\Integrations\WooCommerce\Subjects\OrderSubject; |
| 18 | use MailPoet\Automation\Integrations\WooCommerce\WooCommerce; |
| 19 | use MailPoet\Validator\Builder; |
| 20 | use MailPoet\Validator\Schema\ObjectSchema; |
| 21 | |
| 22 | class OrderStatusChangedTrigger implements Trigger { |
| 23 | |
| 24 | /** @var WordPress */ |
| 25 | protected $wp; |
| 26 | |
| 27 | /** @var WooCommerce */ |
| 28 | protected $woocommerce; |
| 29 | |
| 30 | public function __construct( |
| 31 | WordPress $wp, |
| 32 | WooCommerce $woocommerceHelper |
| 33 | ) { |
| 34 | $this->wp = $wp; |
| 35 | $this->woocommerce = $woocommerceHelper; |
| 36 | } |
| 37 | |
| 38 | public function getKey(): string { |
| 39 | return 'woocommerce:order-status-changed'; |
| 40 | } |
| 41 | |
| 42 | public function getName(): string { |
| 43 | // translators: automation trigger title |
| 44 | return __('Order status changed', 'mailpoet'); |
| 45 | } |
| 46 | |
| 47 | public function getArgsSchema(): ObjectSchema { |
| 48 | return Builder::object([ |
| 49 | 'from' => Builder::string()->required()->default('any'), |
| 50 | 'to' => Builder::string()->required()->default('wc-completed'), |
| 51 | ]); |
| 52 | } |
| 53 | |
| 54 | public function getSubjectKeys(): array { |
| 55 | return [ |
| 56 | OrderSubject::KEY, |
| 57 | OrderStatusChangeSubject::KEY, |
| 58 | CustomerSubject::KEY, |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | public function validate(StepValidationArgs $args): void { |
| 63 | } |
| 64 | |
| 65 | public function registerHooks(): void { |
| 66 | $this->wp->addAction( |
| 67 | 'woocommerce_order_status_changed', |
| 68 | [ |
| 69 | $this, |
| 70 | 'handle', |
| 71 | ], |
| 72 | 10, |
| 73 | 3 |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | public function handle(int $orderId, string $oldStatus, string $newStatus): void { |
| 78 | $order = $this->woocommerce->wcGetOrder($orderId); |
| 79 | if (!$order instanceof \WC_Order) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | $this->wp->doAction(Hooks::TRIGGER, $this, [ |
| 84 | new Subject(OrderStatusChangeSubject::KEY, ['from' => $oldStatus, 'to' => $newStatus]), |
| 85 | new Subject(OrderSubject::KEY, ['order_id' => $order->get_id()]), |
| 86 | new Subject(CustomerSubject::KEY, ['customer_id' => $order->get_customer_id(), 'order_id' => $order->get_id()]), |
| 87 | ]); |
| 88 | } |
| 89 | |
| 90 | public function isTriggeredBy(StepRunArgs $args): bool { |
| 91 | /** @var OrderStatusChangePayload $orderPayload */ |
| 92 | $orderPayload = $args->getSinglePayloadByClass(OrderStatusChangePayload::class); |
| 93 | $triggerArgs = $args->getStep()->getArgs(); |
| 94 | $configuredFrom = $triggerArgs['from'] ? str_replace('wc-', '', $triggerArgs['from']) : null; |
| 95 | $configuredTo = $triggerArgs['to'] ? str_replace('wc-', '', $triggerArgs['to']) : null; |
| 96 | if ($configuredFrom !== 'any' && $orderPayload->getFrom() !== $configuredFrom) { |
| 97 | return false; |
| 98 | } |
| 99 | if ($configuredTo !== 'any' && $orderPayload->getTo() !== $configuredTo) { |
| 100 | return false; |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | } |
| 105 |