AbandonedCartPayload.php
2 years ago
CustomerPayload.php
1 year ago
OrderPayload.php
1 year ago
OrderStatusChangePayload.php
3 years ago
index.php
3 years ago
CustomerPayload.php
145 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\WooCommerce\Payloads; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Integration\Payload; |
| 9 | use WC_Customer; |
| 10 | use WC_Order; |
| 11 | |
| 12 | class CustomerPayload implements Payload { |
| 13 | private ?WC_Customer $customer; |
| 14 | private ?WC_Order $order; |
| 15 | |
| 16 | public function __construct( |
| 17 | ?WC_Customer $customer = null, |
| 18 | ?WC_Order $order = null |
| 19 | ) { |
| 20 | $this->customer = $customer; |
| 21 | $this->order = $order; |
| 22 | } |
| 23 | |
| 24 | public function getBillingCompany(): ?string { |
| 25 | if ($this->isGuest()) { |
| 26 | return $this->order ? (string)$this->order->get_billing_company() : null; |
| 27 | } |
| 28 | return (string)$this->customer->get_billing_company(); |
| 29 | } |
| 30 | |
| 31 | public function getBillingPhone(): ?string { |
| 32 | if ($this->isGuest()) { |
| 33 | return $this->order ? (string)$this->order->get_billing_phone() : null; |
| 34 | } |
| 35 | return (string)$this->customer->get_billing_phone(); |
| 36 | } |
| 37 | |
| 38 | public function getBillingCity(): ?string { |
| 39 | if ($this->isGuest()) { |
| 40 | return $this->order ? (string)$this->order->get_billing_city() : null; |
| 41 | } |
| 42 | return (string)$this->customer->get_billing_city(); |
| 43 | } |
| 44 | |
| 45 | public function getBillingPostcode(): ?string { |
| 46 | if ($this->isGuest()) { |
| 47 | return $this->order ? (string)$this->order->get_billing_postcode() : null; |
| 48 | } |
| 49 | return (string)$this->customer->get_billing_postcode(); |
| 50 | } |
| 51 | |
| 52 | public function getBillingState(): ?string { |
| 53 | if ($this->isGuest()) { |
| 54 | return $this->order ? (string)$this->order->get_billing_state() : null; |
| 55 | } |
| 56 | return (string)$this->customer->get_billing_state(); |
| 57 | } |
| 58 | |
| 59 | public function getBillingCountry(): ?string { |
| 60 | if ($this->isGuest()) { |
| 61 | return $this->order ? (string)$this->order->get_billing_country() : null; |
| 62 | } |
| 63 | return (string)$this->customer->get_billing_country(); |
| 64 | } |
| 65 | |
| 66 | public function getShippingCompany(): ?string { |
| 67 | if ($this->isGuest()) { |
| 68 | return $this->order ? (string)$this->order->get_shipping_company() : null; |
| 69 | } |
| 70 | return (string)$this->customer->get_shipping_company(); |
| 71 | } |
| 72 | |
| 73 | public function getShippingPhone(): ?string { |
| 74 | if ($this->isGuest()) { |
| 75 | return $this->order ? (string)$this->order->get_shipping_phone() : null; |
| 76 | } |
| 77 | return (string)$this->customer->get_shipping_phone(); |
| 78 | } |
| 79 | |
| 80 | public function getShippingCity(): ?string { |
| 81 | if ($this->isGuest()) { |
| 82 | return $this->order ? (string)$this->order->get_shipping_city() : null; |
| 83 | } |
| 84 | return (string)$this->customer->get_shipping_city(); |
| 85 | } |
| 86 | |
| 87 | public function getShippingPostcode(): ?string { |
| 88 | if ($this->isGuest()) { |
| 89 | return $this->order ? (string)$this->order->get_shipping_postcode() : null; |
| 90 | } |
| 91 | return (string)$this->customer->get_shipping_postcode(); |
| 92 | } |
| 93 | |
| 94 | public function getShippingState(): ?string { |
| 95 | if ($this->isGuest()) { |
| 96 | return $this->order ? (string)$this->order->get_shipping_state() : null; |
| 97 | } |
| 98 | return (string)$this->customer->get_shipping_state(); |
| 99 | } |
| 100 | |
| 101 | public function getShippingCountry(): ?string { |
| 102 | if ($this->isGuest()) { |
| 103 | return $this->order ? (string)$this->order->get_shipping_country() : null; |
| 104 | } |
| 105 | return (string)$this->customer->get_shipping_country(); |
| 106 | } |
| 107 | |
| 108 | public function getTotalSpent(): float { |
| 109 | if ($this->isGuest()) { |
| 110 | return $this->order && $this->order->is_paid() ? (float)$this->order->get_total() : 0.0; |
| 111 | } |
| 112 | return (float)$this->customer->get_total_spent(); |
| 113 | } |
| 114 | |
| 115 | public function getAverageSpent(): float { |
| 116 | $totalSpent = $this->getTotalSpent(); |
| 117 | $orderCount = $this->getOrderCount(); |
| 118 | return $orderCount > 0 ? ($totalSpent / $orderCount) : 0.0; |
| 119 | } |
| 120 | |
| 121 | public function getOrderCount(): int { |
| 122 | if ($this->isGuest()) { |
| 123 | return $this->order ? 1 : 0; |
| 124 | } |
| 125 | return (int)$this->customer->get_order_count(); |
| 126 | } |
| 127 | |
| 128 | public function getCustomer(): ?WC_Customer { |
| 129 | return $this->customer; |
| 130 | } |
| 131 | |
| 132 | public function getOrder(): ?WC_Order { |
| 133 | return $this->order; |
| 134 | } |
| 135 | |
| 136 | public function getId(): int { |
| 137 | return $this->customer ? $this->customer->get_id() : 0; |
| 138 | } |
| 139 | |
| 140 | /** @phpstan-assert-if-true null $this->customer */ |
| 141 | public function isGuest(): bool { |
| 142 | return $this->customer === null; |
| 143 | } |
| 144 | } |
| 145 |