Track purchases regardless of status but return revenue status correctly

When we apply this commit every purchase will be tracked. When the shop manager updates an order
the status will be updated. For the statistics it will only return values for purchases with
the 'completed' status

[MAILPOET-5485]
This commit is contained in:
David Remer
2023-08-24 10:21:54 +03:00
committed by Aschepikov
parent bba7786e7e
commit 7ac2065b1a
4 changed files with 38 additions and 2 deletions

View File

@@ -52,7 +52,6 @@ class WooCommercePastOrders extends SimpleWorker {
}, 10, 2);
$orderIds = $this->woocommerceHelper->wcGetOrders([
'status' => $this->woocommerceHelper->getPurchaseStates(),
'date_completed' => '>=' . (($createdAt = $oldestClick->getCreatedAt()) ? $createdAt->format('Y-m-d H:i:s') : null),
'orderby' => 'ID',
'order' => 'ASC',

View File

@@ -263,6 +263,8 @@ class NewsletterStatisticsRepository extends Repository {
return null;
}
$revenueStatus = $this->wcHelper->getPurchaseStates();
$currency = $this->wcHelper->getWoocommerceCurrency();
$query = $this->entityManager
->createQueryBuilder()
@@ -270,8 +272,10 @@ class NewsletterStatisticsRepository extends Repository {
->from(StatisticsWooCommercePurchaseEntity::class, 'stats')
->where('stats.newsletter IN (:newsletters)')
->andWhere('stats.orderCurrency = :currency')
->andWhere('stats.status IN (:revenue_status)')
->setParameter('newsletters', $newsletters)
->setParameter('currency', $currency)
->setParameter('revenue_status', $revenueStatus)
->groupBy('stats.newsletter');
if ($from && $to) {

View File

@@ -7,11 +7,27 @@ use MailPoet\Entities\NewsletterEntity;
use MailPoet\Entities\SendingQueueEntity;
use MailPoet\Entities\StatisticsClickEntity;
use MailPoet\Entities\StatisticsWooCommercePurchaseEntity;
use MailPoet\WooCommerce\Helper;
use MailPoetVendor\Doctrine\DBAL\Connection;
use MailPoetVendor\Doctrine\DBAL\ParameterType;
use MailPoetVendor\Doctrine\ORM\EntityManager;
/**
* @extends Repository<StatisticsWooCommercePurchaseEntity>
*/
class StatisticsWooCommercePurchasesRepository extends Repository {
/** @var Helper */
private $wooCommerceHelper;
public function __construct(
EntityManager $entityManager,
Helper $wooCommerceHelper
) {
parent::__construct($entityManager);
$this->wooCommerceHelper = $wooCommerceHelper;
}
protected function getEntityClassName() {
return StatisticsWooCommercePurchaseEntity::class;
}
@@ -50,6 +66,7 @@ class StatisticsWooCommercePurchasesRepository extends Repository {
}
public function getRevenuesByCampaigns(string $currency): array {
$revenueStatus = $this->wooCommerceHelper->getPurchaseStates();
$revenueStatsTable = $this->entityManager->getClassMetadata(StatisticsWooCommercePurchaseEntity::class)->getTableName();
$newsletterTable = $this->entityManager->getClassMetadata(NewsletterEntity::class)->getTableName();
@@ -72,12 +89,19 @@ class StatisticsWooCommercePurchasesRepository extends Repository {
n.id = swp.newsletter_id
WHERE
swp.order_currency = :currency
AND swp.status IN (:revenue_status)
AND swp.click_id IN (SELECT MIN(click_id) FROM ' . $revenueStatsTable . ' ss GROUP BY order_id)
GROUP BY campaign_id, n.type;
', [
'notification_history_type' => NewsletterEntity::TYPE_NOTIFICATION_HISTORY,
'notification_type' => NewsletterEntity::TYPE_NOTIFICATION,
'currency' => $currency,
'revenue_status' => $revenueStatus,
], [
'notification_history_type' => ParameterType::STRING,
'notification_type' => ParameterType::STRING,
'currency' => ParameterType::STRING,
'revenue_status' => Connection::PARAM_STR_ARRAY,
])->fetchAllAssociative();
$data = array_map(function($row) {

View File

@@ -52,7 +52,16 @@ class WooCommercePurchases {
public function trackPurchase($id, $useCookies = true) {
$order = $this->woocommerceHelper->wcGetOrder($id);
if (!$order instanceof WC_Order || !in_array($order->get_status(), $this->woocommerceHelper->getPurchaseStates(), true)) {
if (!$order instanceof WC_Order) {
return;
}
$statistics = $this->statisticsWooCommercePurchasesRepository->findOneBy(['orderId' => $order->get_id()]);
if ($statistics && $statistics->getClick()) {
$this->statisticsWooCommercePurchasesRepository->createOrUpdateByClickDataAndOrder(
$statistics->getClick(),
$order
);
return;
}