Apply "in the last X" parameter to fields backfilled from orders for guests

[PREMIUM-253]
This commit is contained in:
Jan Jakes
2024-03-11 12:05:36 +01:00
committed by Aschepikov
parent 29083b1f0f
commit 7df37c88f6

View File

@@ -9,6 +9,7 @@ use MailPoet\Automation\Engine\WordPress;
use MailPoet\Automation\Integrations\WooCommerce\Payloads\CustomerPayload; use MailPoet\Automation\Integrations\WooCommerce\Payloads\CustomerPayload;
use MailPoet\Automation\Integrations\WooCommerce\WooCommerce; use MailPoet\Automation\Integrations\WooCommerce\WooCommerce;
use WC_Customer; use WC_Customer;
use WC_Order;
use WC_Order_Item_Product; use WC_Order_Item_Product;
use WC_Product; use WC_Product;
@@ -47,7 +48,11 @@ class CustomerOrderFieldsFactory {
function (CustomerPayload $payload, array $params = []) { function (CustomerPayload $payload, array $params = []) {
$customer = $payload->getCustomer(); $customer = $payload->getCustomer();
$inTheLastSeconds = isset($params['in_the_last']) ? (int)$params['in_the_last'] : null; $inTheLastSeconds = isset($params['in_the_last']) ? (int)$params['in_the_last'] : null;
return $inTheLastSeconds === null || !$customer if (!$customer) {
$order = $payload->getOrder();
return $order && $this->isInTheLastSeconds($order, $inTheLastSeconds) ? $payload->getTotalSpent() : 0.0;
}
return $inTheLastSeconds === null
? $payload->getTotalSpent() ? $payload->getTotalSpent()
: $this->getRecentSpentTotal($customer, $inTheLastSeconds); : $this->getRecentSpentTotal($customer, $inTheLastSeconds);
}, },
@@ -62,7 +67,13 @@ class CustomerOrderFieldsFactory {
function (CustomerPayload $payload, array $params = []) { function (CustomerPayload $payload, array $params = []) {
$customer = $payload->getCustomer(); $customer = $payload->getCustomer();
$inTheLastSeconds = isset($params['in_the_last']) ? (int)$params['in_the_last'] : null; $inTheLastSeconds = isset($params['in_the_last']) ? (int)$params['in_the_last'] : null;
if ($inTheLastSeconds === null || !$customer) {
if (!$customer) {
$order = $payload->getOrder();
return $order && $this->isInTheLastSeconds($order, $inTheLastSeconds) ? $payload->getAverageSpent() : 0.0;
}
if ($inTheLastSeconds === null) {
return $payload->getAverageSpent(); return $payload->getAverageSpent();
} else { } else {
$totalSpent = $this->getRecentSpentTotal($customer, $inTheLastSeconds); $totalSpent = $this->getRecentSpentTotal($customer, $inTheLastSeconds);
@@ -81,7 +92,11 @@ class CustomerOrderFieldsFactory {
function (CustomerPayload $payload, array $params = []) { function (CustomerPayload $payload, array $params = []) {
$customer = $payload->getCustomer(); $customer = $payload->getCustomer();
$inTheLastSeconds = isset($params['in_the_last']) ? (int)$params['in_the_last'] : null; $inTheLastSeconds = isset($params['in_the_last']) ? (int)$params['in_the_last'] : null;
return $inTheLastSeconds === null || !$customer if (!$customer) {
$order = $payload->getOrder();
return $order && $this->isInTheLastSeconds($order, $inTheLastSeconds) ? $payload->getOrderCount() : 0;
}
return $inTheLastSeconds === null
? $payload->getOrderCount() ? $payload->getOrderCount()
: $this->getRecentOrderCount($customer, $inTheLastSeconds); : $this->getRecentOrderCount($customer, $inTheLastSeconds);
}, },
@@ -124,7 +139,7 @@ class CustomerOrderFieldsFactory {
$inTheLastSeconds = isset($params['in_the_last']) ? (int)$params['in_the_last'] : null; $inTheLastSeconds = isset($params['in_the_last']) ? (int)$params['in_the_last'] : null;
if (!$customer) { if (!$customer) {
$order = $payload->getOrder(); $order = $payload->getOrder();
$items = $order && $order->is_paid() ? $order->get_items() : []; $items = $order && $order->is_paid() && $this->isInTheLastSeconds($order, $inTheLastSeconds) ? $order->get_items() : [];
$ids = []; $ids = [];
foreach ($items as $item) { foreach ($items as $item) {
$product = $item instanceof WC_Order_Item_Product ? $item->get_product() : null; $product = $item instanceof WC_Order_Item_Product ? $item->get_product() : null;
@@ -152,7 +167,7 @@ class CustomerOrderFieldsFactory {
$inTheLastSeconds = isset($params['in_the_last']) ? (int)$params['in_the_last'] : null; $inTheLastSeconds = isset($params['in_the_last']) ? (int)$params['in_the_last'] : null;
if (!$customer) { if (!$customer) {
$order = $payload->getOrder(); $order = $payload->getOrder();
$items = $order && $order->is_paid() ? $order->get_items() : []; $items = $order && $order->is_paid() && $this->isInTheLastSeconds($order, $inTheLastSeconds) ? $order->get_items() : [];
$ids = []; $ids = [];
foreach ($items as $item) { foreach ($items as $item) {
$product = $item instanceof WC_Order_Item_Product ? $item->get_product() : null; $product = $item instanceof WC_Order_Item_Product ? $item->get_product() : null;
@@ -333,4 +348,11 @@ class CustomerOrderFieldsFactory {
return array_map('intval', $wpdb->get_col($statement)); return array_map('intval', $wpdb->get_col($statement));
} }
private function isInTheLastSeconds(WC_Order $order, ?int $inTheLastSeconds): bool {
if ($inTheLastSeconds === null) {
return true;
}
return $order->get_date_created() >= new DateTimeImmutable("-$inTheLastSeconds seconds");
}
} }