Add WC revenues to NewsletterStatistics
[MAILPOET-2503]
This commit is contained in:
committed by
Jack Kitterhing
parent
efc2342190
commit
65c8ddf6c2
@@ -16,11 +16,15 @@ class NewsletterStatistics {
|
|||||||
/** @var int */
|
/** @var int */
|
||||||
private $totalSentCount;
|
private $totalSentCount;
|
||||||
|
|
||||||
public function __construct($clickCount, $openCount, $unsubscribeCount, $totalSentCount) {
|
/** @var NewsletterWooCommerceRevenue|null */
|
||||||
|
private $wooCommerceRevenue;
|
||||||
|
|
||||||
|
public function __construct($clickCount, $openCount, $unsubscribeCount, $totalSentCount, $wooCommerceRevenue) {
|
||||||
$this->clickCount = $clickCount;
|
$this->clickCount = $clickCount;
|
||||||
$this->openCount = $openCount;
|
$this->openCount = $openCount;
|
||||||
$this->unsubscribeCount = $unsubscribeCount;
|
$this->unsubscribeCount = $unsubscribeCount;
|
||||||
$this->totalSentCount = $totalSentCount;
|
$this->totalSentCount = $totalSentCount;
|
||||||
|
$this->wooCommerceRevenue = $wooCommerceRevenue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,4 +55,11 @@ class NewsletterStatistics {
|
|||||||
return $this->totalSentCount;
|
return $this->totalSentCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return NewsletterWooCommerceRevenue|null
|
||||||
|
*/
|
||||||
|
public function getWooCommerceRevenue() {
|
||||||
|
return $this->wooCommerceRevenue;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -8,9 +8,21 @@ use MailPoet\Entities\ScheduledTaskEntity;
|
|||||||
use MailPoet\Entities\StatisticsClickEntity;
|
use MailPoet\Entities\StatisticsClickEntity;
|
||||||
use MailPoet\Entities\StatisticsOpenEntity;
|
use MailPoet\Entities\StatisticsOpenEntity;
|
||||||
use MailPoet\Entities\StatisticsUnsubscribeEntity;
|
use MailPoet\Entities\StatisticsUnsubscribeEntity;
|
||||||
|
use MailPoet\Entities\StatisticsWooCommercePurchaseEntity;
|
||||||
|
use MailPoet\WooCommerce\Helper as WCHelper;
|
||||||
|
use MailPoetVendor\Doctrine\ORM\EntityManager;
|
||||||
use MailPoetVendor\Doctrine\ORM\UnexpectedResultException;
|
use MailPoetVendor\Doctrine\ORM\UnexpectedResultException;
|
||||||
|
|
||||||
class NewsletterStatisticsRepository extends Repository {
|
class NewsletterStatisticsRepository extends Repository {
|
||||||
|
|
||||||
|
/** @var WCHelper */
|
||||||
|
private $wcHelper;
|
||||||
|
|
||||||
|
public function __construct(EntityManager $entity_manager, WCHelper $wcHelper) {
|
||||||
|
parent::__construct($entity_manager);
|
||||||
|
$this->wcHelper = $wcHelper;
|
||||||
|
}
|
||||||
|
|
||||||
protected function getEntityClassName() {
|
protected function getEntityClassName() {
|
||||||
return NewsletterEntity::class;
|
return NewsletterEntity::class;
|
||||||
}
|
}
|
||||||
@@ -46,7 +58,8 @@ class NewsletterStatisticsRepository extends Repository {
|
|||||||
$this->getStatisticsClickCount($newsletter),
|
$this->getStatisticsClickCount($newsletter),
|
||||||
$this->getStatisticsOpenCount($newsletter),
|
$this->getStatisticsOpenCount($newsletter),
|
||||||
$this->getStatisticsUnsubscribeCount($newsletter),
|
$this->getStatisticsUnsubscribeCount($newsletter),
|
||||||
$this->getTotalSentCount($newsletter)
|
$this->getTotalSentCount($newsletter),
|
||||||
|
$this->getWooCommerceRevenue($newsletter)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +87,30 @@ class NewsletterStatisticsRepository extends Repository {
|
|||||||
return $this->getStatisticsCount($newsletter, StatisticsUnsubscribeEntity::class);
|
return $this->getStatisticsCount($newsletter, StatisticsUnsubscribeEntity::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getWooCommerceRevenue(NewsletterEntity $newsletter) {
|
||||||
|
if (!$this->wcHelper->isWooCommerceActive()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$currency = $this->wcHelper->getWoocommerceCurrency();
|
||||||
|
list($data) = $this->entityManager
|
||||||
|
->createQueryBuilder()
|
||||||
|
->select('SUM(stats.order_price_total) AS total, COUNT(stats.id) AS cnt')
|
||||||
|
->from(StatisticsWooCommercePurchaseEntity::class, 'stats')
|
||||||
|
->where('stats.newsletter = :newsletter')
|
||||||
|
->andWhere('stats.order_currency = :currency')
|
||||||
|
->setParameter('newsletter', $newsletter)
|
||||||
|
->setParameter('currency', $currency)
|
||||||
|
->getQuery()
|
||||||
|
->getResult();
|
||||||
|
$value = (float)$data['total'];
|
||||||
|
$count = (int)$data['cnt'];
|
||||||
|
return new NewsletterWooCommerceRevenue($currency, $value, $count, $this->wcHelper);
|
||||||
|
} catch (UnexpectedResultException $e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function getStatisticsCount(NewsletterEntity $newsletter, $statisticsEntityName) {
|
private function getStatisticsCount(NewsletterEntity $newsletter, $statisticsEntityName) {
|
||||||
try {
|
try {
|
||||||
$qb = $this->entityManager
|
$qb = $this->entityManager
|
||||||
|
48
lib/Newsletter/Statistics/NewsletterWooCommerceRevenue.php
Normal file
48
lib/Newsletter/Statistics/NewsletterWooCommerceRevenue.php
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace MailPoet\Newsletter\Statistics;
|
||||||
|
|
||||||
|
use MailPoet\WooCommerce\Helper;
|
||||||
|
|
||||||
|
class NewsletterWooCommerceRevenue {
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
private $currency;
|
||||||
|
|
||||||
|
/** @var float */
|
||||||
|
private $value;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
private $ordersCount;
|
||||||
|
|
||||||
|
/** @var Helper */
|
||||||
|
private $wooCommerceHelper;
|
||||||
|
|
||||||
|
public function __construct($currency, $value, $ordersCount, Helper $wooCommerceHelper) {
|
||||||
|
$this->currency = $currency;
|
||||||
|
$this->value = $value;
|
||||||
|
$this->ordersCount = $ordersCount;
|
||||||
|
$this->wooCommerceHelper = $wooCommerceHelper;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return string */
|
||||||
|
public function getCurrency() {
|
||||||
|
return $this->currency;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return int */
|
||||||
|
public function getOrdersCount() {
|
||||||
|
return $this->ordersCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return float */
|
||||||
|
public function getValue() {
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return string */
|
||||||
|
public function getFormattedValue() {
|
||||||
|
return $this->wooCommerceHelper->getRawPrice($this->value, ['currency' => $this->getCurrency()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user