Prevent tracking WooCommerce purchases multiple times
[MAILPOET-2446]
This commit is contained in:
committed by
Jack Kitterhing
parent
260b9baeae
commit
d2f6c48acb
@ -16,9 +16,13 @@ use WC_Order;
|
||||
class StatisticsWooCommercePurchases extends Model {
|
||||
public static $_table = MP_STATISTICS_WOOCOMMERCE_PURCHASES_TABLE;
|
||||
|
||||
static function createOrUpdateByClickAndOrder(StatisticsClicks $click, WC_Order $order) {
|
||||
$statistics = self::where('click_id', $click->id)
|
||||
->where('order_id', $order->get_id())
|
||||
static function createOrUpdateByClickDataAndOrder(StatisticsClicks $click, WC_Order $order) {
|
||||
// search by subscriber and newsletter IDs (instead of click itself) to avoid duplicities
|
||||
// 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();
|
||||
|
||||
if (!$statistics) {
|
||||
|
@ -38,7 +38,7 @@ class WooCommercePurchases {
|
||||
$processed_newsletter_ids_map = [];
|
||||
$order_email_clicks = $this->getClicks($order->get_billing_email(), $from, $to);
|
||||
foreach ($order_email_clicks as $click) {
|
||||
StatisticsWooCommercePurchases::createOrUpdateByClickAndOrder($click, $order);
|
||||
StatisticsWooCommercePurchases::createOrUpdateByClickDataAndOrder($click, $order);
|
||||
$processed_newsletter_ids_map[$click->newsletter_id] = true;
|
||||
}
|
||||
|
||||
@ -52,7 +52,7 @@ class WooCommercePurchases {
|
||||
if (isset($processed_newsletter_ids_map[$click->newsletter_id])) {
|
||||
continue; // do not track click for newsletters that were already tracked by order email
|
||||
}
|
||||
StatisticsWooCommercePurchases::createOrUpdateByClickAndOrder($click, $order);
|
||||
StatisticsWooCommercePurchases::createOrUpdateByClickDataAndOrder($click, $order);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -138,6 +138,17 @@ class WooCommercePurchasesTest extends \MailPoetTest {
|
||||
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() {
|
||||
$this->createClick($this->link, $this->subscriber, 20);
|
||||
$order_mock = $this->createOrderMock($this->subscriber->email);
|
||||
|
Reference in New Issue
Block a user