diff --git a/mailpoet/assets/js/src/automation/integrations/mailpoet/analytics/formatter/index.tsx b/mailpoet/assets/js/src/automation/integrations/mailpoet/analytics/formatter/index.tsx new file mode 100644 index 0000000000..24df413f08 --- /dev/null +++ b/mailpoet/assets/js/src/automation/integrations/mailpoet/analytics/formatter/index.tsx @@ -0,0 +1,7 @@ +import CurrencyFactory from "@woocommerce/currency/build"; +import {MailPoet} from "../../../../../mailpoet"; + +export function formattedPrice(price: number) : string { + const storeCurrency = CurrencyFactory(MailPoet.WooCommerceStoreConfig); + return storeCurrency.formatAmount(price); +} diff --git a/mailpoet/assets/js/src/global.d.ts b/mailpoet/assets/js/src/global.d.ts index 0dc8ad7d3f..5ef1d2166e 100644 --- a/mailpoet/assets/js/src/global.d.ts +++ b/mailpoet/assets/js/src/global.d.ts @@ -82,6 +82,16 @@ declare module '@woocommerce/blocks-checkout' { function registerCheckoutBlock(options: CheckoutBlockOptions): void; } +type WooCommerceStoreConfig = { + precision: number | string | null; + decimalSeparator: string; + thousandSeparator: string; + code: string; + symbol: string; + symbolPosition: 'left' | 'right' | 'left_space' | 'right_space'; + priceFormat?: string; +} | undefined + interface Window { ajaxurl: string; mailpoet_token: string; @@ -91,6 +101,7 @@ interface Window { mailpoet_premium_version: string; mailpoet_premium_link: string; mailpoet_woocommerce_active: boolean; + mailpoet_woocommerce_store_config: WooCommerceStoreConfig; mailpoet_woocommerce_version: string; mailpoet_track_wizard_loaded_via_woocommerce: boolean; mailpoet_premium_active: boolean; diff --git a/mailpoet/assets/js/src/mailpoet.ts b/mailpoet/assets/js/src/mailpoet.ts index 51d98b3db8..3372259209 100644 --- a/mailpoet/assets/js/src/mailpoet.ts +++ b/mailpoet/assets/js/src/mailpoet.ts @@ -22,6 +22,7 @@ export const MailPoet = { premiumVersion: window.mailpoet_premium_version, premiumLink: window.mailpoet_premium_link, isWoocommerceActive: window.mailpoet_woocommerce_active, + WooCommerceStoreConfig: window.mailpoet_woocommerce_store_config, premiumActive: window.mailpoet_premium_active, subscribersLimit: window.mailpoet_subscribers_limit, subscribersLimitReached: window.mailpoet_subscribers_limit_reached, diff --git a/mailpoet/lib/AdminPages/PageRenderer.php b/mailpoet/lib/AdminPages/PageRenderer.php index d8de7b7218..414a130160 100644 --- a/mailpoet/lib/AdminPages/PageRenderer.php +++ b/mailpoet/lib/AdminPages/PageRenderer.php @@ -182,6 +182,7 @@ class PageRenderer { 'automationEditor' => admin_url('admin.php?page=mailpoet-automation-editor'), 'automationTemplates' => admin_url('admin.php?page=mailpoet-automation-templates'), ], + 'woocommerce_store_config' => $this->woocommerceStoreConfig(), 'tags' => array_map(function (TagEntity $tag): array { return [ 'id' => $tag->getId(), @@ -214,4 +215,18 @@ class PageRenderer { $notice->displayWPNotice(); } } + + private function woocommerceStoreConfig() { + + return [ + 'precision' => wc_get_price_decimals(), + 'decimalSeparator' => wc_get_price_decimal_separator(), + 'thousandSeparator' => wc_get_price_thousand_separator(), + 'code' => get_woocommerce_currency(), + 'symbol' => html_entity_decode(get_woocommerce_currency_symbol()), + 'symbolPosition' => get_option('woocommerce_currency_pos'), + 'priceFormat' => get_woocommerce_price_format(), + + ]; + } } diff --git a/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Controller/OverviewStatisticsController.php b/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Controller/OverviewStatisticsController.php index ed5ba37a59..e4e75b1632 100644 --- a/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Controller/OverviewStatisticsController.php +++ b/mailpoet/lib/Automation/Integrations/MailPoet/Analytics/Controller/OverviewStatisticsController.php @@ -87,6 +87,13 @@ 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']); } $previousStatistics = $this->newsletterStatisticsRepository->getBatchStatistics( @@ -109,6 +116,12 @@ 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( diff --git a/mailpoet/package.json b/mailpoet/package.json index 195f463624..2b219a51cc 100644 --- a/mailpoet/package.json +++ b/mailpoet/package.json @@ -35,6 +35,7 @@ "@marvelapp/react-ab-test": "^3.1.0", "@types/select2": "^4.0.55", "@woocommerce/components": "^10.0.0", + "@woocommerce/currency": "^4.2.0", "@woocommerce/date": "^4.2.0", "@wordpress/a11y": "^3.28.0", "@wordpress/api-fetch": "^6.25.0", diff --git a/mailpoet/views/layout.html b/mailpoet/views/layout.html index f4f23ce971..1e7f226a23 100644 --- a/mailpoet/views/layout.html +++ b/mailpoet/views/layout.html @@ -54,6 +54,7 @@ var mailpoet_analytics_new_public_id = <%= is_analytics_public_id_new() | json_encode %>; var mailpoet_free_domains = <%= json_encode(mailpoet_free_domains()) %>; var mailpoet_woocommerce_active = <%= json_encode(is_woocommerce_active()) %>; + var mailpoet_woocommerce_store_config = <%= json_encode(woocommerce_store_config) %>; // RFC 5322 standard; http://emailregex.com/ combined with https://google.github.io/closure-library/api/goog.format.EmailAddress.html#isValid var mailpoet_email_regex = /(?=^[+a-zA-Z0-9_.!#$%&'*\/=?^`{|}~-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,63}$)(?=^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,})))/; var mailpoet_feature_flags = <%= json_encode(feature_flags) %>; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cf61119359..36eef0e0d6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -64,6 +64,9 @@ importers: '@woocommerce/components': specifier: ^10.0.0 version: 10.0.0(patch_hash=prtmohnt73dk73q3vnzuiaioji)(@babel/core@7.17.8)(@types/react@18.0.28)(@wordpress/core-data@6.5.0)(@wordpress/data@8.5.0)(lodash@4.17.21)(react-dom@18.2.0)(react@18.2.0) + '@woocommerce/currency': + specifier: ^4.2.0 + version: 4.2.0 '@woocommerce/date': specifier: ^4.2.0 version: 4.2.0(lodash@4.17.21) @@ -9791,6 +9794,16 @@ packages: '@wordpress/i18n': 3.20.0 dev: false + /@woocommerce/currency@4.2.0: + resolution: {integrity: sha512-ZbYWKl6fbSU5hCYDFZ1WrONeDlTbDixELkyWgnmv6bAHANiOH5T4lhFcDHs+s8gA80FzxS01YpHZRbJhLrMcaA==} + dependencies: + '@woocommerce/number': 2.4.0 + '@wordpress/deprecated': 2.12.3 + '@wordpress/element': 4.20.0 + '@wordpress/html-entities': 3.28.0 + '@wordpress/i18n': 3.20.0 + dev: false + /@woocommerce/data@3.1.0(@babel/core@7.17.8)(@types/react@18.0.28)(@wordpress/core-data@6.5.0)(lodash@4.17.21)(moment@2.29.4)(react-dom@18.2.0)(react@18.2.0): resolution: {integrity: sha512-YIf04NhbMWp+ySkH6NSNASPJWXIZi0LeU5aVvYl6wCOQQp3RP/mgzNINBdxemcsm/QI2Y7zWGeHTREZlOr2kuw==} peerDependencies: @@ -9878,6 +9891,12 @@ packages: locutus: 2.0.16 dev: false + /@woocommerce/number@2.4.0: + resolution: {integrity: sha512-SmEgad2zZ9fGZtpujrdk0/toSXjNuDOUxDKQutJ++ASID17bGKZtLxXcu/E7mvF1tTgszpiToPvqZlyC+vjOlg==} + dependencies: + locutus: 2.0.16 + dev: false + /@wordpress/a11y@3.28.0: resolution: {integrity: sha512-nXreWPc1vSBP0ak5cx8ZTZDmVj0DCqC32cCK/RP2J2ANpIQJ6PILO199CodicQhM6dXBAkXk0H7IBCgsqLG+Ng==} engines: {node: '>=12'}