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
AbandonedCartSubject.php
77 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\Field; |
| 9 | use MailPoet\Automation\Engine\Data\Subject as SubjectData; |
| 10 | use MailPoet\Automation\Engine\Exceptions\InvalidStateException; |
| 11 | use MailPoet\Automation\Engine\Integration\Payload; |
| 12 | use MailPoet\Automation\Engine\Integration\Subject; |
| 13 | use MailPoet\Automation\Integrations\WooCommerce\Payloads\AbandonedCartPayload; |
| 14 | use MailPoet\Automation\Integrations\WooCommerce\WooCommerce; |
| 15 | use MailPoet\Validator\Builder; |
| 16 | use MailPoet\Validator\Schema\ObjectSchema; |
| 17 | |
| 18 | /** |
| 19 | * @implements Subject<AbandonedCartPayload> |
| 20 | */ |
| 21 | class AbandonedCartSubject implements Subject { |
| 22 | const KEY = 'woocommerce:abandoned_cart'; |
| 23 | |
| 24 | /** @var WooCommerce */ |
| 25 | private $woocommerceHelper; |
| 26 | |
| 27 | public function __construct( |
| 28 | WooCommerce $woocommerceHelper |
| 29 | ) { |
| 30 | $this->woocommerceHelper = $woocommerceHelper; |
| 31 | } |
| 32 | |
| 33 | public function getKey(): string { |
| 34 | return self::KEY; |
| 35 | } |
| 36 | |
| 37 | public function getName(): string { |
| 38 | // translators: automation subject (entity entering automation) title |
| 39 | return __('WooCommerce abandoned cart', 'mailpoet'); |
| 40 | } |
| 41 | |
| 42 | public function getArgsSchema(): ObjectSchema { |
| 43 | return Builder::object([ |
| 44 | 'user_id' => Builder::integer()->required(), |
| 45 | 'last_activity_at' => Builder::string()->required()->default(30), |
| 46 | 'product_ids' => Builder::array(Builder::integer())->required(), |
| 47 | ]); |
| 48 | } |
| 49 | |
| 50 | public function getPayload(SubjectData $subjectData): Payload { |
| 51 | if (!$this->woocommerceHelper->isWooCommerceActive()) { |
| 52 | throw InvalidStateException::create()->withMessage('WooCommerce is not active'); |
| 53 | } |
| 54 | $lastActivityAt = \DateTimeImmutable::createFromFormat(\DateTime::W3C, $subjectData->getArgs()['last_activity_at']); |
| 55 | if (!$lastActivityAt) { |
| 56 | throw InvalidStateException::create()->withMessage('Invalid abandoned cart time'); |
| 57 | } |
| 58 | |
| 59 | $customer = new \WC_Customer($subjectData->getArgs()['user_id']); |
| 60 | |
| 61 | return new AbandonedCartPayload($customer, $lastActivityAt, $subjectData->getArgs()['product_ids']); |
| 62 | } |
| 63 | |
| 64 | public function getFields(): array { |
| 65 | return [ |
| 66 | new Field( |
| 67 | 'woocommerce:cart:cart-total', |
| 68 | Field::TYPE_NUMBER, |
| 69 | __('Cart total', 'mailpoet'), |
| 70 | function (AbandonedCartPayload $payload) { |
| 71 | return $payload->getTotal(); |
| 72 | } |
| 73 | ), |
| 74 | ]; |
| 75 | } |
| 76 | } |
| 77 |