diff --git a/lib/Cron/Workers/WooCommercePastOrders.php b/lib/Cron/Workers/WooCommercePastOrders.php index 064f98ce15..d1fbb78955 100644 --- a/lib/Cron/Workers/WooCommercePastOrders.php +++ b/lib/Cron/Workers/WooCommercePastOrders.php @@ -3,8 +3,8 @@ namespace MailPoet\Cron\Workers; use MailPoet\Entities\ScheduledTaskEntity; -use MailPoet\Models\StatisticsClicks; -use MailPoet\Models\StatisticsWooCommercePurchases; +use MailPoet\Entities\StatisticsClickEntity; +use MailPoet\Statistics\StatisticsClicksRepository; use MailPoet\Statistics\Track\WooCommercePurchases; use MailPoet\WooCommerce\Helper as WCHelper; use MailPoetVendor\Carbon\Carbon; @@ -19,12 +19,17 @@ class WooCommercePastOrders extends SimpleWorker { /** @var WooCommercePurchases */ private $woocommercePurchases; + /** @var StatisticsClicksRepository */ + private $statisticsClicksRepository; + public function __construct( WCHelper $woocommerceHelper, + StatisticsClicksRepository $statisticsClicksRepository, WooCommercePurchases $woocommercePurchases ) { $this->woocommerceHelper = $woocommerceHelper; $this->woocommercePurchases = $woocommercePurchases; + $this->statisticsClicksRepository = $statisticsClicksRepository; parent::__construct(); } @@ -33,8 +38,8 @@ class WooCommercePastOrders extends SimpleWorker { } public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { - $oldestClick = StatisticsClicks::orderByAsc('created_at')->limit(1)->findOne(); - if (!$oldestClick instanceof StatisticsClicks) { + $oldestClick = $this->statisticsClicksRepository->findOneBy([], ['createdAt' => 'asc']); + if (!$oldestClick instanceof StatisticsClickEntity) { return true; } @@ -48,7 +53,7 @@ class WooCommercePastOrders extends SimpleWorker { $orderIds = $this->woocommerceHelper->wcGetOrders([ 'status' => 'completed', - 'date_completed' => '>=' . $oldestClick->createdAt, + 'date_completed' => '>=' . $oldestClick->getCreatedAt()->format('Y-m-d H:i:s'), 'orderby' => 'ID', 'order' => 'ASC', 'limit' => self::BATCH_SIZE, diff --git a/lib/Statistics/StatisticsWooCommercePurchasesRepository.php b/lib/Statistics/StatisticsWooCommercePurchasesRepository.php index dad328c320..d604de02e6 100644 --- a/lib/Statistics/StatisticsWooCommercePurchasesRepository.php +++ b/lib/Statistics/StatisticsWooCommercePurchasesRepository.php @@ -3,6 +3,7 @@ namespace MailPoet\Statistics; use MailPoet\Doctrine\Repository; +use MailPoet\Entities\StatisticsClickEntity; use MailPoet\Entities\StatisticsWooCommercePurchaseEntity; /** @@ -12,4 +13,32 @@ class StatisticsWooCommercePurchasesRepository extends Repository { protected function getEntityClassName() { return StatisticsWooCommercePurchaseEntity::class; } + + public function createOrUpdateByClickDataAndOrder(StatisticsClickEntity $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 = $this->findOneBy([ + 'orderId' => $order->get_id(), + 'subscriber' => $click->getSubscriber(), + 'newsletter' => $click->getNewsletter(), + ]); + + if (!$statistics instanceof StatisticsWooCommercePurchaseEntity) { + $statistics = new StatisticsWooCommercePurchaseEntity( + $click->getNewsletter(), + $click->getQueue(), + $click, + $order->get_id(), + $order->get_currency(), + $order->get_total() + ); + $this->persist($statistics); + } else { + $statistics->setOrderCurrency($order->get_currency()); + $statistics->setOrderPriceTotal($order->get_total()); + } + $statistics->setSubscriber($click->getSubscriber()); + $this->flush(); + } }