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,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();
}
}