AbandonedCartSubject.php
2 years ago
CustomerSubject.php
5 months ago
OrderStatusChangeSubject.php
2 years ago
OrderSubject.php
2 years ago
index.php
3 years ago
OrderSubject.php
68 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\WooCommerce\Subjects; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Subject as SubjectData; |
| 9 | use MailPoet\Automation\Engine\Integration\Payload; |
| 10 | use MailPoet\Automation\Engine\Integration\Subject; |
| 11 | use MailPoet\Automation\Integrations\WooCommerce\Fields\OrderFieldsFactory; |
| 12 | use MailPoet\Automation\Integrations\WooCommerce\Payloads\OrderPayload; |
| 13 | use MailPoet\Automation\Integrations\WooCommerce\WooCommerce; |
| 14 | use MailPoet\NotFoundException; |
| 15 | use MailPoet\Validator\Builder; |
| 16 | use MailPoet\Validator\Schema\ObjectSchema; |
| 17 | |
| 18 | /** |
| 19 | * @implements Subject<OrderPayload> |
| 20 | */ |
| 21 | class OrderSubject implements Subject { |
| 22 | |
| 23 | const KEY = 'woocommerce:order'; |
| 24 | |
| 25 | /** @var WooCommerce */ |
| 26 | private $woocommerce; |
| 27 | |
| 28 | /** @var OrderFieldsFactory */ |
| 29 | private $orderFieldsFactory; |
| 30 | |
| 31 | public function __construct( |
| 32 | OrderFieldsFactory $orderFieldsFactory, |
| 33 | WooCommerce $woocommerce |
| 34 | ) { |
| 35 | $this->woocommerce = $woocommerce; |
| 36 | $this->orderFieldsFactory = $orderFieldsFactory; |
| 37 | } |
| 38 | |
| 39 | public function getName(): string { |
| 40 | // translators: automation subject (entity entering automation) title |
| 41 | return __('WooCommerce order', 'mailpoet'); |
| 42 | } |
| 43 | |
| 44 | public function getArgsSchema(): ObjectSchema { |
| 45 | return Builder::object([ |
| 46 | 'order_id' => Builder::integer()->required(), |
| 47 | ]); |
| 48 | } |
| 49 | |
| 50 | public function getPayload(SubjectData $subjectData): Payload { |
| 51 | $id = $subjectData->getArgs()['order_id']; |
| 52 | $order = $this->woocommerce->wcGetOrder($id); |
| 53 | if (!$order instanceof \WC_Order) { |
| 54 | // translators: %d is the order ID. |
| 55 | throw NotFoundException::create()->withMessage(sprintf(__("Order with ID '%d' not found.", 'mailpoet'), $id)); |
| 56 | } |
| 57 | return new OrderPayload($order); |
| 58 | } |
| 59 | |
| 60 | public function getKey(): string { |
| 61 | return self::KEY; |
| 62 | } |
| 63 | |
| 64 | public function getFields(): array { |
| 65 | return $this->orderFieldsFactory->getFields(); |
| 66 | } |
| 67 | } |
| 68 |