Format prices in frontend
[MAILPOET-5092]
This commit is contained in:
@ -8,6 +8,7 @@ import { select, useSelect } from '@wordpress/data';
|
||||
import { MailPoet } from '../../../../../../mailpoet';
|
||||
import { OverviewSection, storeName } from '../../store';
|
||||
import { locale } from '../../../../../config';
|
||||
import {formattedPrice} from "../../formatter";
|
||||
|
||||
function getEmailPercentage(
|
||||
type: 'opened' | 'clicked',
|
||||
@ -119,7 +120,7 @@ export function Overview(): JSX.Element | null {
|
||||
key="overview-revenue"
|
||||
label={__('Revenue', 'mailpoet')}
|
||||
delta={getWooCommerceDelta('revenue').toFixed(2) as unknown as number}
|
||||
value={overview.data?.revenue_formatted?.current}
|
||||
value={formattedPrice(overview.data !== undefined ? overview.data.revenue.current : 0)}
|
||||
/>,
|
||||
);
|
||||
}
|
||||
|
@ -1,12 +1,13 @@
|
||||
import {EmailStats} from "../../../store";
|
||||
import {__, sprintf} from "@wordpress/i18n";
|
||||
import {EmailStats} from "../../../store";
|
||||
import {Actions} from "./actions";
|
||||
import {locale} from "../../../../../../config";
|
||||
import {Cell} from "./cell";
|
||||
import {formattedPrice} from "../../../formatter";
|
||||
|
||||
const percentageFormatter = Intl.NumberFormat(locale.toString(), { style: 'percent', maximumFractionDigits: 2 });
|
||||
|
||||
function calculatePercentage(value: number, base: number, canBeNegative: boolean = false) : number {
|
||||
function calculatePercentage(value: number, base: number, canBeNegative = false) : number {
|
||||
if (base === 0) {
|
||||
return 0;
|
||||
}
|
||||
@ -17,7 +18,9 @@ function calculatePercentage(value: number, base: number, canBeNegative: boolean
|
||||
function percentageBadgeCalculation(percentage:number) : {badge: string, badgeType: string} {
|
||||
if (percentage > 3) {
|
||||
return {badge: __('Excellent', 'mailpoet'), badgeType: 'mailpoet-analytics-badge-success'}
|
||||
} else if (percentage > 1) {
|
||||
}
|
||||
|
||||
if (percentage > 1) {
|
||||
return {badge: __('Good', 'mailpoet'), badgeType: 'mailpoet-analytics-badge-success'}
|
||||
}
|
||||
return {badge: __('Average', 'mailpoet'), badgeType: 'mailpoet-analytics-badge-warning'}
|
||||
@ -78,7 +81,7 @@ export function transformEmailsToRows(emails: EmailStats[]) {
|
||||
},
|
||||
{
|
||||
display: <Cell
|
||||
value={email.revenue_formatted.current}
|
||||
value={formattedPrice(email.revenue.current)}
|
||||
/>,
|
||||
value: email.revenue.current
|
||||
},
|
||||
|
@ -22,7 +22,6 @@ export type EmailStats = {
|
||||
clicked: CurrentAndPrevious;
|
||||
orders: CurrentAndPrevious;
|
||||
revenue: CurrentAndPrevious;
|
||||
revenue_formatted: CurrentAndPrevious;
|
||||
unsubscribed: CurrentAndPrevious;
|
||||
};
|
||||
|
||||
@ -32,7 +31,6 @@ type OverviewSectionData = SectionData & {
|
||||
orders: CurrentAndPrevious;
|
||||
unsubscribed: CurrentAndPrevious;
|
||||
revenue: CurrentAndPrevious;
|
||||
revenue_formatted: CurrentAndPrevious;
|
||||
sent: CurrentAndPrevious;
|
||||
emails: Record<string, EmailStats>;
|
||||
};
|
||||
|
@ -11,7 +11,6 @@ use MailPoet\Entities\StatisticsOpenEntity;
|
||||
use MailPoet\Newsletter\NewslettersRepository;
|
||||
use MailPoet\Newsletter\Statistics\NewsletterStatisticsRepository;
|
||||
use MailPoet\Newsletter\Statistics\WooCommerceRevenue;
|
||||
use MailPoet\WooCommerce\Helper;
|
||||
|
||||
class OverviewStatisticsController {
|
||||
/** @var NewslettersRepository */
|
||||
@ -20,27 +19,16 @@ class OverviewStatisticsController {
|
||||
/** @var NewsletterStatisticsRepository */
|
||||
private $newsletterStatisticsRepository;
|
||||
|
||||
/** @var Helper */
|
||||
private $wooCommerceHelper;
|
||||
|
||||
public function __construct(
|
||||
NewslettersRepository $newslettersRepository,
|
||||
NewsletterStatisticsRepository $newsletterStatisticsRepository,
|
||||
Helper $wooCommerceHelper
|
||||
NewsletterStatisticsRepository $newsletterStatisticsRepository
|
||||
) {
|
||||
$this->newslettersRepository = $newslettersRepository;
|
||||
$this->newsletterStatisticsRepository = $newsletterStatisticsRepository;
|
||||
$this->wooCommerceHelper = $wooCommerceHelper;
|
||||
}
|
||||
|
||||
public function getStatisticsForAutomation(Automation $automation, Query $query): array {
|
||||
$emails = $this->getEmailsFromAutomation($automation);
|
||||
$formattedEmptyRevenue = $this->wooCommerceHelper->getRawPrice(
|
||||
0,
|
||||
[
|
||||
'currency' => $this->wooCommerceHelper->getWoocommerceCurrency(),
|
||||
]
|
||||
);
|
||||
$data = [
|
||||
'sent' => ['current' => 0, 'previous' => 0],
|
||||
'opened' => ['current' => 0, 'previous' => 0],
|
||||
@ -48,10 +36,6 @@ class OverviewStatisticsController {
|
||||
'orders' => ['current' => 0, 'previous' => 0],
|
||||
'unsubscribed' => ['current' => 0, 'previous' => 0],
|
||||
'revenue' => ['current' => 0, 'previous' => 0],
|
||||
'revenue_formatted' => [
|
||||
'current' => $formattedEmptyRevenue,
|
||||
'previous' => $formattedEmptyRevenue,
|
||||
],
|
||||
'emails' => [],
|
||||
];
|
||||
if (!$emails) {
|
||||
@ -87,12 +71,6 @@ class OverviewStatisticsController {
|
||||
$data['emails'][$newsletterId]['unsubscribed']['current'] = $statistic->getUnsubscribeCount();
|
||||
$data['emails'][$newsletterId]['orders']['current'] = $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getOrdersCount() : 0;
|
||||
$data['emails'][$newsletterId]['revenue']['current'] = $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getValue() : 0;
|
||||
$data['emails'][$newsletterId]['revenue_formatted']['current'] = $this->wooCommerceHelper->getRawPrice(
|
||||
$data['emails'][$newsletterId]['revenue']['current'],
|
||||
[
|
||||
'currency' => $this->wooCommerceHelper->getWoocommerceCurrency(),
|
||||
]
|
||||
);
|
||||
$data['emails'][$newsletterId]['order'] = count($data['emails']);
|
||||
}
|
||||
|
||||
@ -116,28 +94,8 @@ class OverviewStatisticsController {
|
||||
$data['emails'][$newsletterId]['unsubscribed']['previous'] = $statistic->getUnsubscribeCount();
|
||||
$data['emails'][$newsletterId]['orders']['previous'] = $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getOrdersCount() : 0;
|
||||
$data['emails'][$newsletterId]['revenue']['previous'] = $statistic->getWooCommerceRevenue() ? $statistic->getWooCommerceRevenue()->getValue() : 0;
|
||||
$data['emails'][$newsletterId]['revenue_formatted']['current'] = $this->wooCommerceHelper->getRawPrice(
|
||||
$data['emails'][$newsletterId]['revenue']['current'],
|
||||
[
|
||||
'currency' => $this->wooCommerceHelper->getWoocommerceCurrency(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$data['revenue_formatted']['current'] = $this->wooCommerceHelper->getRawPrice(
|
||||
$data['revenue']['current'],
|
||||
[
|
||||
'currency' => $this->wooCommerceHelper->getWoocommerceCurrency(),
|
||||
]
|
||||
);
|
||||
|
||||
$data['revenue_formatted']['previous'] = $this->wooCommerceHelper->getRawPrice(
|
||||
$data['revenue']['previous'],
|
||||
[
|
||||
'currency' => $this->wooCommerceHelper->getWoocommerceCurrency(),
|
||||
]
|
||||
);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user