diff --git a/lib/API/JSON/v1/SubscriberStats.php b/lib/API/JSON/v1/SubscriberStats.php index c36e6a734d..0ee9ae9c8d 100644 --- a/lib/API/JSON/v1/SubscriberStats.php +++ b/lib/API/JSON/v1/SubscriberStats.php @@ -6,6 +6,7 @@ use MailPoet\API\JSON\Endpoint as APIEndpoint; use MailPoet\API\JSON\Error as APIError; use MailPoet\Config\AccessControl; use MailPoet\Entities\SubscriberEntity; +use MailPoet\Newsletter\Statistics\WooCommerceRevenue; use MailPoet\Subscribers\Statistics\SubscriberStatisticsRepository; use MailPoet\Subscribers\SubscribersRepository; use MailPoet\WP\Functions as WPFunctions; @@ -39,11 +40,16 @@ class SubscriberStats extends APIEndpoint { ]); } $statistics = $this->subscribersStatisticsRepository->getStatistics($subscriber); - return $this->successResponse([ + $response = [ 'email' => $subscriber->getEmail(), 'total_sent' => $statistics->getTotalSentCount(), 'open' => $statistics->getOpenCount(), 'click' => $statistics->getClickCount(), - ]); + ]; + $woocommerce = $statistics->getWooCommerceRevenue(); + if ($woocommerce instanceof WooCommerceRevenue) { + $response['woocommerce'] = $woocommerce->asArray(); + } + return $this->successResponse($response); } } diff --git a/lib/Newsletter/Statistics/WooCommerceRevenue.php b/lib/Newsletter/Statistics/WooCommerceRevenue.php index 4792c73f9f..262792cf79 100644 --- a/lib/Newsletter/Statistics/WooCommerceRevenue.php +++ b/lib/Newsletter/Statistics/WooCommerceRevenue.php @@ -45,6 +45,15 @@ class WooCommerceRevenue { 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 */ @@ -54,6 +63,7 @@ class WooCommerceRevenue { 'value' => (float)$this->value, 'count' => (int)$this->ordersCount, 'formatted' => $this->getFormattedValue(), + 'formatted_average' => $this->getFormattedAverageValue(), ]; } } diff --git a/lib/Subscribers/Statistics/SubscriberStatistics.php b/lib/Subscribers/Statistics/SubscriberStatistics.php index 333abc9017..116b650700 100644 --- a/lib/Subscribers/Statistics/SubscriberStatistics.php +++ b/lib/Subscribers/Statistics/SubscriberStatistics.php @@ -2,6 +2,8 @@ namespace MailPoet\Subscribers\Statistics; +use MailPoet\Newsletter\Statistics\WooCommerceRevenue; + class SubscriberStatistics { /** @var int */ @@ -13,10 +15,14 @@ class SubscriberStatistics { /** @var int */ 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->openCount = $openCount; $this->totalSentCount = $totalSentCount; + $this->wooCommerceRevenue = $wooCommerceRevenue; } /** @@ -39,4 +45,11 @@ class SubscriberStatistics { public function getTotalSentCount(): int { return $this->totalSentCount; } + + /** + * @return WooCommerceRevenue|null + */ + public function getWooCommerceRevenue() { + return $this->wooCommerceRevenue; + } } diff --git a/lib/Subscribers/Statistics/SubscriberStatisticsRepository.php b/lib/Subscribers/Statistics/SubscriberStatisticsRepository.php index b9e127e4cb..c1eca5e5e1 100644 --- a/lib/Subscribers/Statistics/SubscriberStatisticsRepository.php +++ b/lib/Subscribers/Statistics/SubscriberStatisticsRepository.php @@ -6,12 +6,25 @@ use MailPoet\Doctrine\Repository; use MailPoet\Entities\StatisticsClickEntity; use MailPoet\Entities\StatisticsNewsletterEntity; use MailPoet\Entities\StatisticsOpenEntity; +use MailPoet\Entities\StatisticsWooCommercePurchaseEntity; use MailPoet\Entities\SubscriberEntity; +use MailPoet\Newsletter\Statistics\WooCommerceRevenue; +use MailPoet\WooCommerce\Helper as WCHelper; +use MailPoetVendor\Doctrine\ORM\EntityManager; /** * @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() { return SubscriberEntity::class; } @@ -20,7 +33,8 @@ class SubscriberStatisticsRepository extends Repository { return new SubscriberStatistics( $this->getStatisticsClickCount($subscriber), $this->getStatisticsOpenCount($subscriber), - $this->getTotalSentCount($subscriber) + $this->getTotalSentCount($subscriber), + $this->getWooCommerceRevenue($subscriber) ); } @@ -45,4 +59,29 @@ class SubscriberStatisticsRepository extends Repository { ->getQuery() ->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 + ); + } }