Use Doctrine in WooCommercePastOrders cron worker

[MAILPOET-3814]
This commit is contained in:
Pavel Dohnal
2021-10-13 13:25:25 +02:00
committed by Veljko V
parent cfa87a165d
commit 102e333521
2 changed files with 39 additions and 5 deletions

View File

@@ -3,8 +3,8 @@
namespace MailPoet\Cron\Workers; namespace MailPoet\Cron\Workers;
use MailPoet\Entities\ScheduledTaskEntity; use MailPoet\Entities\ScheduledTaskEntity;
use MailPoet\Models\StatisticsClicks; use MailPoet\Entities\StatisticsClickEntity;
use MailPoet\Models\StatisticsWooCommercePurchases; use MailPoet\Statistics\StatisticsClicksRepository;
use MailPoet\Statistics\Track\WooCommercePurchases; use MailPoet\Statistics\Track\WooCommercePurchases;
use MailPoet\WooCommerce\Helper as WCHelper; use MailPoet\WooCommerce\Helper as WCHelper;
use MailPoetVendor\Carbon\Carbon; use MailPoetVendor\Carbon\Carbon;
@@ -19,12 +19,17 @@ class WooCommercePastOrders extends SimpleWorker {
/** @var WooCommercePurchases */ /** @var WooCommercePurchases */
private $woocommercePurchases; private $woocommercePurchases;
/** @var StatisticsClicksRepository */
private $statisticsClicksRepository;
public function __construct( public function __construct(
WCHelper $woocommerceHelper, WCHelper $woocommerceHelper,
StatisticsClicksRepository $statisticsClicksRepository,
WooCommercePurchases $woocommercePurchases WooCommercePurchases $woocommercePurchases
) { ) {
$this->woocommerceHelper = $woocommerceHelper; $this->woocommerceHelper = $woocommerceHelper;
$this->woocommercePurchases = $woocommercePurchases; $this->woocommercePurchases = $woocommercePurchases;
$this->statisticsClicksRepository = $statisticsClicksRepository;
parent::__construct(); parent::__construct();
} }
@@ -33,8 +38,8 @@ class WooCommercePastOrders extends SimpleWorker {
} }
public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { public function processTaskStrategy(ScheduledTaskEntity $task, $timer) {
$oldestClick = StatisticsClicks::orderByAsc('created_at')->limit(1)->findOne(); $oldestClick = $this->statisticsClicksRepository->findOneBy([], ['createdAt' => 'asc']);
if (!$oldestClick instanceof StatisticsClicks) { if (!$oldestClick instanceof StatisticsClickEntity) {
return true; return true;
} }
@@ -48,7 +53,7 @@ class WooCommercePastOrders extends SimpleWorker {
$orderIds = $this->woocommerceHelper->wcGetOrders([ $orderIds = $this->woocommerceHelper->wcGetOrders([
'status' => 'completed', 'status' => 'completed',
'date_completed' => '>=' . $oldestClick->createdAt, 'date_completed' => '>=' . $oldestClick->getCreatedAt()->format('Y-m-d H:i:s'),
'orderby' => 'ID', 'orderby' => 'ID',
'order' => 'ASC', 'order' => 'ASC',
'limit' => self::BATCH_SIZE, 'limit' => self::BATCH_SIZE,

View File

@@ -3,6 +3,7 @@
namespace MailPoet\Statistics; namespace MailPoet\Statistics;
use MailPoet\Doctrine\Repository; use MailPoet\Doctrine\Repository;
use MailPoet\Entities\StatisticsClickEntity;
use MailPoet\Entities\StatisticsWooCommercePurchaseEntity; use MailPoet\Entities\StatisticsWooCommercePurchaseEntity;
/** /**
@@ -12,4 +13,32 @@ class StatisticsWooCommercePurchasesRepository extends Repository {
protected function getEntityClassName() { protected function getEntityClassName() {
return StatisticsWooCommercePurchaseEntity::class; 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();
}
} }