Prevent tracking WooCommerce purchases multiple times

[MAILPOET-2446]
This commit is contained in:
Jan Jakeš
2019-10-09 11:08:24 +02:00
committed by Jack Kitterhing
parent 260b9baeae
commit d2f6c48acb
3 changed files with 20 additions and 5 deletions

View File

@ -16,9 +16,13 @@ use WC_Order;
class StatisticsWooCommercePurchases extends Model { class StatisticsWooCommercePurchases extends Model {
public static $_table = MP_STATISTICS_WOOCOMMERCE_PURCHASES_TABLE; public static $_table = MP_STATISTICS_WOOCOMMERCE_PURCHASES_TABLE;
static function createOrUpdateByClickAndOrder(StatisticsClicks $click, WC_Order $order) { static function createOrUpdateByClickDataAndOrder(StatisticsClicks $click, WC_Order $order) {
$statistics = self::where('click_id', $click->id) // search by subscriber and newsletter IDs (instead of click itself) to avoid duplicities
->where('order_id', $order->get_id()) // when a new click from the subscriber appeared since last tracking for given newsletter
// (this will keep the originally tracked click - likely the click that led to the order)
$statistics = self::where('order_id', $order->get_id())
->where('subscriber_id', $click->subscriber_id)
->where('newsletter_id', $click->newsletter_id)
->findOne(); ->findOne();
if (!$statistics) { if (!$statistics) {

View File

@ -38,7 +38,7 @@ class WooCommercePurchases {
$processed_newsletter_ids_map = []; $processed_newsletter_ids_map = [];
$order_email_clicks = $this->getClicks($order->get_billing_email(), $from, $to); $order_email_clicks = $this->getClicks($order->get_billing_email(), $from, $to);
foreach ($order_email_clicks as $click) { foreach ($order_email_clicks as $click) {
StatisticsWooCommercePurchases::createOrUpdateByClickAndOrder($click, $order); StatisticsWooCommercePurchases::createOrUpdateByClickDataAndOrder($click, $order);
$processed_newsletter_ids_map[$click->newsletter_id] = true; $processed_newsletter_ids_map[$click->newsletter_id] = true;
} }
@ -52,7 +52,7 @@ class WooCommercePurchases {
if (isset($processed_newsletter_ids_map[$click->newsletter_id])) { if (isset($processed_newsletter_ids_map[$click->newsletter_id])) {
continue; // do not track click for newsletters that were already tracked by order email continue; // do not track click for newsletters that were already tracked by order email
} }
StatisticsWooCommercePurchases::createOrUpdateByClickAndOrder($click, $order); StatisticsWooCommercePurchases::createOrUpdateByClickDataAndOrder($click, $order);
} }
} }

View File

@ -138,6 +138,17 @@ class WooCommercePurchasesTest extends \MailPoetTest {
expect(count(StatisticsWooCommercePurchases::findMany()))->equals(1); expect(count(StatisticsWooCommercePurchases::findMany()))->equals(1);
} }
function testItTracksPaymentOnlyOnceWhenNewClickAppears() {
$this->createClick($this->link, $this->subscriber, 5);
$order_mock = $this->createOrderMock($this->subscriber->email);
$woocommerce_purchases = new WooCommercePurchases($this->createWooCommerceHelperMock($order_mock), $this->cookies);
$woocommerce_purchases->trackPurchase($order_mock->get_id());
$this->createClick($this->link, $this->subscriber, 1);
$woocommerce_purchases->trackPurchase($order_mock->get_id());
expect(count(StatisticsWooCommercePurchases::findMany()))->equals(1);
}
function testItDoesNotTrackPaymentWhenClickTooOld() { function testItDoesNotTrackPaymentWhenClickTooOld() {
$this->createClick($this->link, $this->subscriber, 20); $this->createClick($this->link, $this->subscriber, 20);
$order_mock = $this->createOrderMock($this->subscriber->email); $order_mock = $this->createOrderMock($this->subscriber->email);