Update # of orders filter for orders with coupon
MAILPOET-5373
This commit is contained in:
committed by
Aschepikov
parent
09a5a5cbbc
commit
dda7da465f
@ -78,6 +78,12 @@ class WooCommerceNumberOfOrders implements Filter {
|
|||||||
$joinCondition
|
$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', [
|
$queryBuilder->add('join', [
|
||||||
$subscribersTable => [
|
$subscribersTable => [
|
||||||
/**
|
/**
|
||||||
|
@ -19,11 +19,18 @@ class WooCommerceNumberOfOrdersTest extends \MailPoetTest {
|
|||||||
*/
|
*/
|
||||||
private $subscriberFactory;
|
private $subscriberFactory;
|
||||||
|
|
||||||
|
/** @var \WC_Product */
|
||||||
|
private $product;
|
||||||
|
|
||||||
public function _before(): void {
|
public function _before(): void {
|
||||||
$this->subscriberFactory = new SubscriberFactory();
|
$this->subscriberFactory = new SubscriberFactory();
|
||||||
$this->numberOfOrdersFilter = $this->diContainer->get(WooCommerceNumberOfOrders::class);
|
$this->numberOfOrdersFilter = $this->diContainer->get(WooCommerceNumberOfOrders::class);
|
||||||
$this->cleanUp();
|
$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');
|
$customerId1 = $this->tester->createCustomer('customer1@example.com', 'customer');
|
||||||
$customerId2 = $this->tester->createCustomer('customer2@example.com', 'customer');
|
$customerId2 = $this->tester->createCustomer('customer2@example.com', 'customer');
|
||||||
$customerId3 = $this->tester->createCustomer('customer3@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($customerId2, Carbon::now());
|
$this->createOrder($customerId2, Carbon::now());
|
||||||
$this->createOrder($customerId3, Carbon::now());
|
$this->createOrder($customerId3, Carbon::now());
|
||||||
}
|
|
||||||
|
|
||||||
public function testItGetsCustomersThatPlacedTwoOrdersInTheLastDay(): void {
|
|
||||||
$segmentFilterData = $this->getSegmentFilterData('=', 2, 1);
|
$segmentFilterData = $this->getSegmentFilterData('=', 2, 1);
|
||||||
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
|
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
|
||||||
$this->assertEqualsCanonicalizing(['customer2@example.com'], $emails);
|
$this->assertEqualsCanonicalizing(['customer2@example.com'], $emails);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItGetsCustomersThatPlacedZeroOrdersInTheLastDay(): void {
|
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);
|
$segmentFilterData = $this->getSegmentFilterData('=', 0, 1);
|
||||||
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
|
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
|
||||||
$this->assertEqualsCanonicalizing(['customer1@example.com'], $emails);
|
$this->assertEqualsCanonicalizing(['customer1@example.com'], $emails);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItGetsCustomersThatDidNotPlaceTwoOrdersInTheLastWeek(): void {
|
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);
|
$segmentFilterData = $this->getSegmentFilterData('!=', 2, 7);
|
||||||
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
|
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
|
||||||
$this->assertEqualsCanonicalizing(['customer1@example.com', 'customer3@example.com'], $emails);
|
$this->assertEqualsCanonicalizing(['customer1@example.com', 'customer3@example.com'], $emails);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItGetsCustomersThatPlacedAtLeastOneOrderInTheLastWeek(): void {
|
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);
|
$segmentFilterData = $this->getSegmentFilterData('>', 0, 7);
|
||||||
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
|
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
|
||||||
$this->assertEqualsCanonicalizing(['customer1@example.com', 'customer2@example.com', 'customer3@example.com'], $emails);
|
$this->assertEqualsCanonicalizing(['customer1@example.com', 'customer2@example.com', 'customer3@example.com'], $emails);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItGetsNoneCustomerSubscriberInLastDays() {
|
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
|
$createdSub = $this->subscriberFactory
|
||||||
->withSource(Source::API)
|
->withSource(Source::API)
|
||||||
->withCreatedAt(Carbon::now()->subDays(5))
|
->withCreatedAt(Carbon::now()->subDays(5))
|
||||||
@ -69,6 +105,14 @@ class WooCommerceNumberOfOrdersTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testItWorksWithAllTimeTimeframe(): void {
|
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);
|
$segmentFilterData = $this->getSegmentFilterData('>', 0, 0, DynamicSegmentFilterData::TIMEFRAME_ALL_TIME);
|
||||||
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
|
$emails = $this->tester->getSubscriberEmailsMatchingDynamicFilter($segmentFilterData, $this->numberOfOrdersFilter);
|
||||||
$this->assertEqualsCanonicalizing([
|
$this->assertEqualsCanonicalizing([
|
||||||
@ -78,8 +122,23 @@ class WooCommerceNumberOfOrdersTest extends \MailPoetTest {
|
|||||||
], $emails);
|
], $emails);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getSegmentFilterData(string $comparisonType, int $ordersCount, int $days, $timeframe = DynamicSegmentFilterData::TIMEFRAME_IN_THE_LAST): DynamicSegmentFilterData {
|
public function testItWorksWithNumberOfOrdersWithCoupon(): void {
|
||||||
return new DynamicSegmentFilterData(DynamicSegmentFilterData::TYPE_WOOCOMMERCE, WooCommerceNumberOfOrders::ACTION_NUMBER_OF_ORDERS, [
|
$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_type' => $comparisonType,
|
||||||
'number_of_orders_count' => $ordersCount,
|
'number_of_orders_count' => $ordersCount,
|
||||||
'days' => $days,
|
'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 = $this->tester->createWooCommerceOrder();
|
||||||
$order->set_customer_id($customerId);
|
$order->set_customer_id($customerId);
|
||||||
$order->set_date_created($createdAt->toDateTimeString());
|
$order->set_date_created($createdAt->toDateTimeString());
|
||||||
$order->set_status($status);
|
$order->set_status($status);
|
||||||
|
if ($withCoupon) {
|
||||||
|
$order->add_product($this->product);
|
||||||
|
$order->apply_coupon('Coupon');
|
||||||
|
}
|
||||||
$order->save();
|
$order->save();
|
||||||
$this->tester->updateWooOrderStats($order->get_id());
|
$this->tester->updateWooOrderStats($order->get_id());
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user