Pass revenues in stats response
[MAILPOET-3069]
This commit is contained in:
@ -6,6 +6,7 @@ use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
|||||||
use MailPoet\API\JSON\Error as APIError;
|
use MailPoet\API\JSON\Error as APIError;
|
||||||
use MailPoet\Config\AccessControl;
|
use MailPoet\Config\AccessControl;
|
||||||
use MailPoet\Entities\SubscriberEntity;
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
|
use MailPoet\Newsletter\Statistics\WooCommerceRevenue;
|
||||||
use MailPoet\Subscribers\Statistics\SubscriberStatisticsRepository;
|
use MailPoet\Subscribers\Statistics\SubscriberStatisticsRepository;
|
||||||
use MailPoet\Subscribers\SubscribersRepository;
|
use MailPoet\Subscribers\SubscribersRepository;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
@ -39,11 +40,16 @@ class SubscriberStats extends APIEndpoint {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
$statistics = $this->subscribersStatisticsRepository->getStatistics($subscriber);
|
$statistics = $this->subscribersStatisticsRepository->getStatistics($subscriber);
|
||||||
return $this->successResponse([
|
$response = [
|
||||||
'email' => $subscriber->getEmail(),
|
'email' => $subscriber->getEmail(),
|
||||||
'total_sent' => $statistics->getTotalSentCount(),
|
'total_sent' => $statistics->getTotalSentCount(),
|
||||||
'open' => $statistics->getOpenCount(),
|
'open' => $statistics->getOpenCount(),
|
||||||
'click' => $statistics->getClickCount(),
|
'click' => $statistics->getClickCount(),
|
||||||
]);
|
];
|
||||||
|
$woocommerce = $statistics->getWooCommerceRevenue();
|
||||||
|
if ($woocommerce instanceof WooCommerceRevenue) {
|
||||||
|
$response['woocommerce'] = $woocommerce->asArray();
|
||||||
|
}
|
||||||
|
return $this->successResponse($response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,15 @@ class WooCommerceRevenue {
|
|||||||
return $this->wooCommerceHelper->getRawPrice($this->value, ['currency' => $this->currency]);
|
return $this->wooCommerceHelper->getRawPrice($this->value, ['currency' => $this->currency]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return string */
|
||||||
|
public function getFormattedAverageValue(): string {
|
||||||
|
$average = 0;
|
||||||
|
if ($this->ordersCount > 0) {
|
||||||
|
$average = $this->value / $this->ordersCount;
|
||||||
|
}
|
||||||
|
return $this->wooCommerceHelper->getRawPrice($average, ['currency' => $this->currency]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@ -54,6 +63,7 @@ class WooCommerceRevenue {
|
|||||||
'value' => (float)$this->value,
|
'value' => (float)$this->value,
|
||||||
'count' => (int)$this->ordersCount,
|
'count' => (int)$this->ordersCount,
|
||||||
'formatted' => $this->getFormattedValue(),
|
'formatted' => $this->getFormattedValue(),
|
||||||
|
'formatted_average' => $this->getFormattedAverageValue(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace MailPoet\Subscribers\Statistics;
|
namespace MailPoet\Subscribers\Statistics;
|
||||||
|
|
||||||
|
use MailPoet\Newsletter\Statistics\WooCommerceRevenue;
|
||||||
|
|
||||||
class SubscriberStatistics {
|
class SubscriberStatistics {
|
||||||
|
|
||||||
/** @var int */
|
/** @var int */
|
||||||
@ -13,10 +15,14 @@ class SubscriberStatistics {
|
|||||||
/** @var int */
|
/** @var int */
|
||||||
private $totalSentCount;
|
private $totalSentCount;
|
||||||
|
|
||||||
public function __construct($clickCount, $openCount, $totalSentCount) {
|
/** @var WooCommerceRevenue|null */
|
||||||
|
private $wooCommerceRevenue;
|
||||||
|
|
||||||
|
public function __construct($clickCount, $openCount, $totalSentCount, $wooCommerceRevenue = null) {
|
||||||
$this->clickCount = $clickCount;
|
$this->clickCount = $clickCount;
|
||||||
$this->openCount = $openCount;
|
$this->openCount = $openCount;
|
||||||
$this->totalSentCount = $totalSentCount;
|
$this->totalSentCount = $totalSentCount;
|
||||||
|
$this->wooCommerceRevenue = $wooCommerceRevenue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -39,4 +45,11 @@ class SubscriberStatistics {
|
|||||||
public function getTotalSentCount(): int {
|
public function getTotalSentCount(): int {
|
||||||
return $this->totalSentCount;
|
return $this->totalSentCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return WooCommerceRevenue|null
|
||||||
|
*/
|
||||||
|
public function getWooCommerceRevenue() {
|
||||||
|
return $this->wooCommerceRevenue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,25 @@ use MailPoet\Doctrine\Repository;
|
|||||||
use MailPoet\Entities\StatisticsClickEntity;
|
use MailPoet\Entities\StatisticsClickEntity;
|
||||||
use MailPoet\Entities\StatisticsNewsletterEntity;
|
use MailPoet\Entities\StatisticsNewsletterEntity;
|
||||||
use MailPoet\Entities\StatisticsOpenEntity;
|
use MailPoet\Entities\StatisticsOpenEntity;
|
||||||
|
use MailPoet\Entities\StatisticsWooCommercePurchaseEntity;
|
||||||
use MailPoet\Entities\SubscriberEntity;
|
use MailPoet\Entities\SubscriberEntity;
|
||||||
|
use MailPoet\Newsletter\Statistics\WooCommerceRevenue;
|
||||||
|
use MailPoet\WooCommerce\Helper as WCHelper;
|
||||||
|
use MailPoetVendor\Doctrine\ORM\EntityManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @extends Repository<SubscriberEntity>
|
* @extends Repository<SubscriberEntity>
|
||||||
*/
|
*/
|
||||||
class SubscriberStatisticsRepository extends Repository {
|
class SubscriberStatisticsRepository extends Repository {
|
||||||
|
|
||||||
|
/** @var WCHelper */
|
||||||
|
private $wcHelper;
|
||||||
|
|
||||||
|
public function __construct(EntityManager $entityManager, WCHelper $wcHelper) {
|
||||||
|
parent::__construct($entityManager);
|
||||||
|
$this->wcHelper = $wcHelper;
|
||||||
|
}
|
||||||
|
|
||||||
protected function getEntityClassName() {
|
protected function getEntityClassName() {
|
||||||
return SubscriberEntity::class;
|
return SubscriberEntity::class;
|
||||||
}
|
}
|
||||||
@ -20,7 +33,8 @@ class SubscriberStatisticsRepository extends Repository {
|
|||||||
return new SubscriberStatistics(
|
return new SubscriberStatistics(
|
||||||
$this->getStatisticsClickCount($subscriber),
|
$this->getStatisticsClickCount($subscriber),
|
||||||
$this->getStatisticsOpenCount($subscriber),
|
$this->getStatisticsOpenCount($subscriber),
|
||||||
$this->getTotalSentCount($subscriber)
|
$this->getTotalSentCount($subscriber),
|
||||||
|
$this->getWooCommerceRevenue($subscriber)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,4 +59,29 @@ class SubscriberStatisticsRepository extends Repository {
|
|||||||
->getQuery()
|
->getQuery()
|
||||||
->getSingleScalarResult();
|
->getSingleScalarResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getWooCommerceRevenue(SubscriberEntity $subscriber) {
|
||||||
|
if (!$this->wcHelper->isWooCommerceActive()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$currency = $this->wcHelper->getWoocommerceCurrency();
|
||||||
|
$result = $this->entityManager
|
||||||
|
->createQueryBuilder()
|
||||||
|
->select('SUM(stats.orderPriceTotal) AS total, COUNT(stats.id) AS cnt')
|
||||||
|
->from(StatisticsWooCommercePurchaseEntity::class, 'stats')
|
||||||
|
->where('stats.subscriber = :subscriber')
|
||||||
|
->andWhere('stats.orderCurrency = :currency')
|
||||||
|
->setParameter('subscriber', $subscriber)
|
||||||
|
->setParameter('currency', $currency)
|
||||||
|
->getQuery()
|
||||||
|
->getSingleResult();
|
||||||
|
|
||||||
|
return new WooCommerceRevenue(
|
||||||
|
$currency,
|
||||||
|
(float)$result['total'],
|
||||||
|
(int)$result['cnt'],
|
||||||
|
$this->wcHelper
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user