mailpoet
/
lib
/
Automation
/
Integrations
/
WooCommerce
/
Fields
/
CustomerSubscriptionFieldsFactory.php
CustomerFieldsFactory.php
11 months ago
CustomerOrderFieldsFactory.php
1 year ago
CustomerReviewFieldsFactory.php
1 year ago
CustomerSubscriptionFieldsFactory.php
11 months ago
OrderFieldsFactory.php
2 months ago
TermOptionsBuilder.php
2 years ago
TermParentsLoader.php
1 year ago
index.php
3 years ago
CustomerSubscriptionFieldsFactory.php
49 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\WooCommerce\Fields; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Field; |
| 9 | use MailPoet\Automation\Integrations\WooCommerce\Payloads\CustomerPayload; |
| 10 | use MailPoet\WooCommerce\WooCommerceSubscriptions\Helper as WCS; |
| 11 | |
| 12 | class CustomerSubscriptionFieldsFactory { |
| 13 | /** @var WCS */ |
| 14 | private $wcs; |
| 15 | |
| 16 | public function __construct( |
| 17 | WCS $wcs |
| 18 | ) { |
| 19 | $this->wcs = $wcs; |
| 20 | } |
| 21 | |
| 22 | /** @return Field[] */ |
| 23 | public function getFields(): array { |
| 24 | return [ |
| 25 | new Field( |
| 26 | 'woocommerce:customer:active-subscription-count', |
| 27 | Field::TYPE_INTEGER, |
| 28 | __('Active subscriptions count', 'mailpoet'), |
| 29 | function (CustomerPayload $payload) { |
| 30 | $customer = $payload->getCustomer(); |
| 31 | if (!$customer) { |
| 32 | return 0; |
| 33 | } |
| 34 | if (!$this->wcs->isWooCommerceSubscriptionsActive()) { |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | $activeSubscriptions = $this->wcs->wcsGetSubscriptions([ |
| 39 | 'customer_id' => $customer->get_id(), |
| 40 | 'subscription_status' => ['active', 'pending-cancel'], |
| 41 | 'limit' => -1, |
| 42 | ]); |
| 43 | return count($activeSubscriptions); |
| 44 | } |
| 45 | ), |
| 46 | ]; |
| 47 | } |
| 48 | } |
| 49 |