Update # of orders filter for orders with coupon

MAILPOET-5373
This commit is contained in:
John Oleksowicz
2023-12-06 11:07:55 -06:00
committed by Aschepikov
parent 09a5a5cbbc
commit dda7da465f
2 changed files with 75 additions and 6 deletions

View File

@ -78,6 +78,12 @@ class WooCommerceNumberOfOrders implements Filter {
$joinCondition
);
$action = $filterData->getAction();
if ($action === self::ACTION_NUMBER_OF_ORDERS_WITH_COUPON) {
$subQuery->innerJoin('orderStats', $wpdb->prefix . 'wc_order_coupon_lookup', 'couponLookup', 'orderStats.order_id = couponLookup.order_id');
}
$queryBuilder->add('join', [
$subscribersTable => [
/**

View File

@ -19,11 +19,18 @@ class WooCommerceNumberOfOrdersTest extends \MailPoetTest {
*/
private $subscriberFactory;
/** @var \WC_Product */
private $product;
public function _before(): void {
$this->subscriberFactory = new SubscriberFactory();
$this->numberOfOrdersFilter = $this->diContainer->get(WooCommerceNumberOfOrders::class);
$this->cleanUp();
$this->product = $this->tester->createWooCommerceProduct(['price' => 20]);
$this->tester->createWooCommerceCoupon(['code' => 'Coupon']);
}
public function testItGetsCustomersThatPlacedTwoOrdersInTheLastDay(): void {
$customerId1 = $this->tester->createCustomer('customer1@example.com', 'customer');
$customerId2 = $this->tester->createCustomer('customer2@example.com', 'customer');
$customerId3 = $this->tester->createCustomer('customer3@example.com', 'customer');
@ -32,33 +39,62 @@ class WooCommerceNumberOfOrdersTest extends \MailPoetTest {
$this->createOrder($customerId2, Carbon::now());
$this->createOrder($customerId2, Carbon::now());
$this->createOrder($customerId3, Carbon::now());
}
public function testItGetsCustomersThatPlacedTwoOrdersInTheLastDay(): void {
$segmentFilterData = $this->getSegmentFilterData('=', 2, 1);
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
$this->assertEqualsCanonicalizing(['customer2@example.com'], $emails);
}
public function testItGetsCustomersThatPlacedZeroOrdersInTheLastDay(): void {
$customerId1 = $this->tester->createCustomer('customer1@example.com', 'customer');
$customerId2 = $this->tester->createCustomer('customer2@example.com', 'customer');
$customerId3 = $this->tester->createCustomer('customer3@example.com', 'customer');
$this->createOrder($customerId1, Carbon::now()->subDays(3));
$this->createOrder($customerId2, Carbon::now());
$this->createOrder($customerId2, Carbon::now());
$this->createOrder($customerId3, Carbon::now());
$segmentFilterData = $this->getSegmentFilterData('=', 0, 1);
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
$this->assertEqualsCanonicalizing(['customer1@example.com'], $emails);
}
public function testItGetsCustomersThatDidNotPlaceTwoOrdersInTheLastWeek(): void {
$customerId1 = $this->tester->createCustomer('customer1@example.com', 'customer');
$customerId2 = $this->tester->createCustomer('customer2@example.com', 'customer');
$customerId3 = $this->tester->createCustomer('customer3@example.com', 'customer');
$this->createOrder($customerId1, Carbon::now()->subDays(3));
$this->createOrder($customerId2, Carbon::now());
$this->createOrder($customerId2, Carbon::now());
$this->createOrder($customerId3, Carbon::now());
$segmentFilterData = $this->getSegmentFilterData('!=', 2, 7);
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
$this->assertEqualsCanonicalizing(['customer1@example.com', 'customer3@example.com'], $emails);
}
public function testItGetsCustomersThatPlacedAtLeastOneOrderInTheLastWeek(): void {
$customerId1 = $this->tester->createCustomer('customer1@example.com', 'customer');
$customerId2 = $this->tester->createCustomer('customer2@example.com', 'customer');
$customerId3 = $this->tester->createCustomer('customer3@example.com', 'customer');
$this->createOrder($customerId1, Carbon::now()->subDays(3));
$this->createOrder($customerId2, Carbon::now());
$this->createOrder($customerId2, Carbon::now());
$this->createOrder($customerId3, Carbon::now());
$segmentFilterData = $this->getSegmentFilterData('>', 0, 7);
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
$this->assertEqualsCanonicalizing(['customer1@example.com', 'customer2@example.com', 'customer3@example.com'], $emails);
}
public function testItGetsNoneCustomerSubscriberInLastDays() {
$customerId1 = $this->tester->createCustomer('customer1@example.com', 'customer');
$customerId2 = $this->tester->createCustomer('customer2@example.com', 'customer');
$customerId3 = $this->tester->createCustomer('customer3@example.com', 'customer');
$this->createOrder($customerId1, Carbon::now()->subDays(3));
$this->createOrder($customerId2, Carbon::now());
$this->createOrder($customerId2, Carbon::now());
$this->createOrder($customerId3, Carbon::now());
$createdSub = $this->subscriberFactory
->withSource(Source::API)
->withCreatedAt(Carbon::now()->subDays(5))
@ -69,6 +105,14 @@ class WooCommerceNumberOfOrdersTest extends \MailPoetTest {
}
public function testItWorksWithAllTimeTimeframe(): void {
$customerId1 = $this->tester->createCustomer('customer1@example.com', 'customer');
$customerId2 = $this->tester->createCustomer('customer2@example.com', 'customer');
$customerId3 = $this->tester->createCustomer('customer3@example.com', 'customer');
$this->createOrder($customerId1, Carbon::now()->subDays(3));
$this->createOrder($customerId2, Carbon::now());
$this->createOrder($customerId2, Carbon::now());
$this->createOrder($customerId3, Carbon::now());
$segmentFilterData = $this->getSegmentFilterData('>', 0, 0, DynamicSegmentFilterData::TIMEFRAME_ALL_TIME);
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
$this->assertEqualsCanonicalizing([
@ -78,8 +122,23 @@ class WooCommerceNumberOfOrdersTest extends \MailPoetTest {
], $emails);
}
private function getSegmentFilterData(string $comparisonType, int $ordersCount, int $days, $timeframe = DynamicSegmentFilterData::TIMEFRAME_IN_THE_LAST): DynamicSegmentFilterData {
return new DynamicSegmentFilterData(DynamicSegmentFilterData::TYPE_WOOCOMMERCE, WooCommerceNumberOfOrders::ACTION_NUMBER_OF_ORDERS, [
public function testItWorksWithNumberOfOrdersWithCoupon(): void {
$customerWithoutCouponOrder = $this->tester->createCustomer('customerwithoutcoupon@example.com');
$this->createOrder($customerWithoutCouponOrder, Carbon::now()->subDays(2));
$segmentFilterData = $this->getSegmentFilterData('>', 0, 7, 'inTheLast', WooCommerceNumberOfOrders::ACTION_NUMBER_OF_ORDERS_WITH_COUPON);
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
$this->assertEqualsCanonicalizing([], $emails);
$customerWithCouponOrder = $this->tester->createCustomer('customer-with-coupon-order@example.com');
$this->createOrder($customerWithCouponOrder, Carbon::now()->subDays(2), true);
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
$this->assertEqualsCanonicalizing(['customer-with-coupon-order@example.com'], $emails);
}
private function getSegmentFilterData(string $comparisonType, int $ordersCount, int $days, $timeframe = DynamicSegmentFilterData::TIMEFRAME_IN_THE_LAST, $action = WooCommerceNumberOfOrders::ACTION_NUMBER_OF_ORDERS): DynamicSegmentFilterData {
return new DynamicSegmentFilterData(DynamicSegmentFilterData::TYPE_WOOCOMMERCE, $action, [
'number_of_orders_type' => $comparisonType,
'number_of_orders_count' => $ordersCount,
'days' => $days,
@ -87,11 +146,15 @@ class WooCommerceNumberOfOrdersTest extends \MailPoetTest {
]);
}
private function createOrder(int $customerId, Carbon $createdAt, $status = 'wc-completed'): int {
private function createOrder(int $customerId, Carbon $createdAt, $withCoupon = false, $status = 'wc-completed'): int {
$order = $this->tester->createWooCommerceOrder();
$order->set_customer_id($customerId);
$order->set_date_created($createdAt->toDateTimeString());
$order->set_status($status);
if ($withCoupon) {
$order->add_product($this->product);
$order->apply_coupon('Coupon');
}
$order->save();
$this->tester->updateWooOrderStats($order->get_id());