mailpoet
/
lib
/
Automation
/
Integrations
/
WooCommerce
/
Triggers
/
Orders
/
OrderCreatedTrigger.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
OrderCreatedTrigger.php
109 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\Subjects\CustomerSubject; |
| 15 | use MailPoet\Automation\Integrations\WooCommerce\Subjects\OrderSubject; |
| 16 | use MailPoet\Validator\Builder; |
| 17 | use MailPoet\Validator\Schema\ObjectSchema; |
| 18 | |
| 19 | class OrderCreatedTrigger implements Trigger { |
| 20 | |
| 21 | /** @var WordPress */ |
| 22 | private $wp; |
| 23 | |
| 24 | /** @var int[] */ |
| 25 | private $processedOrders = []; |
| 26 | |
| 27 | public function __construct( |
| 28 | WordPress $wp |
| 29 | ) { |
| 30 | $this->wp = $wp; |
| 31 | } |
| 32 | |
| 33 | public function getKey(): string { |
| 34 | return 'woocommerce:order-created'; |
| 35 | } |
| 36 | |
| 37 | public function getName(): string { |
| 38 | // translators: automation trigger title |
| 39 | return __('Order created', 'mailpoet'); |
| 40 | } |
| 41 | |
| 42 | public function registerHooks(): void { |
| 43 | $this->wp->addAction( |
| 44 | 'woocommerce_new_order', |
| 45 | [ |
| 46 | $this, |
| 47 | 'handleCreate', |
| 48 | ], |
| 49 | 10, |
| 50 | 2 |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param int $orderId |
| 56 | * @param \WC_Order $order |
| 57 | * @return void |
| 58 | */ |
| 59 | public function handleCreate($orderId, $order) { |
| 60 | |
| 61 | if (in_array($orderId, $this->processedOrders)) { |
| 62 | return; |
| 63 | } |
| 64 | /** |
| 65 | * Creating an order via wc_create_order() does not yet set crucial information like the customer's email address. |
| 66 | * It just creates the order object and saves it to the database. We need therefore to wait for the order to have at least the billing address stored. |
| 67 | **/ |
| 68 | if (!$order->get_billing_email()) { |
| 69 | $this->wp->addAction( |
| 70 | 'woocommerce_after_order_object_save', |
| 71 | function($order) use ($orderId) { |
| 72 | if ((int)$orderId !== (int)$order->get_id()) { |
| 73 | return; |
| 74 | } |
| 75 | $this->handleCreate($order->get_id(), $order); |
| 76 | } |
| 77 | ); |
| 78 | return; |
| 79 | } |
| 80 | $this->processedOrders[] = $orderId; |
| 81 | $this->wp->doAction(Hooks::TRIGGER, $this, [ |
| 82 | new Subject(OrderSubject::KEY, ['order_id' => $order->get_id()]), |
| 83 | new Subject(CustomerSubject::KEY, ['customer_id' => $order->get_customer_id(), 'order_id' => $order->get_id()]), |
| 84 | ]); |
| 85 | } |
| 86 | |
| 87 | public function isTriggeredBy(StepRunArgs $args): bool { |
| 88 | /** |
| 89 | * If we come to this point we always want to trigger the automation. |
| 90 | * The evaluation whether this is a "new" order is done in the handleCreate() method. |
| 91 | */ |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | public function getArgsSchema(): ObjectSchema { |
| 96 | return Builder::object(); |
| 97 | } |
| 98 | |
| 99 | public function getSubjectKeys(): array { |
| 100 | return [ |
| 101 | OrderSubject::KEY, |
| 102 | CustomerSubject::KEY, |
| 103 | ]; |
| 104 | } |
| 105 | |
| 106 | public function validate(StepValidationArgs $args): void { |
| 107 | } |
| 108 | } |
| 109 |