Skip to content

Commit

Permalink
MWPW-152672: bring latest from develop
Browse files Browse the repository at this point in the history
  • Loading branch information
3ch023 committed Jun 19, 2024
1 parent 61f3782 commit f712f43
Show file tree
Hide file tree
Showing 11 changed files with 1,159 additions and 15 deletions.
58 changes: 58 additions & 0 deletions commerce/src/inline-price.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import {
updatePlaceholder,
} from './placeholder.js';
import { selectOffers, useService } from './utilities.js';
import { GeoMap } from './settings';

// countries where tax is displayed for all segments by default
const DISPLAY_ALL_TAX_COUNTRIES = [GeoMap.uk, GeoMap.au, GeoMap.fr, GeoMap.at, GeoMap.be_en, GeoMap.be_fr, GeoMap.be_nl, GeoMap.bg, GeoMap.ch_de, GeoMap.ch_fr, GeoMap.ch_it,
GeoMap.cz, GeoMap.de, GeoMap.dk, GeoMap.ee, GeoMap.eg_ar, GeoMap.eg_en, GeoMap.es, GeoMap.fi, GeoMap.fr, GeoMap.gr_el, GeoMap.gr_en, GeoMap.hu, GeoMap.ie, GeoMap.it, GeoMap.lu_de,
GeoMap.lu_en, GeoMap.lu_fr, GeoMap.nl, GeoMap.no, GeoMap.pl, GeoMap.pt, GeoMap.ro, GeoMap.se, GeoMap.si, GeoMap.sk, GeoMap.tr, GeoMap.ua, GeoMap.id_en, GeoMap.id_id,
GeoMap.in_en, GeoMap.in_hi, GeoMap.jp, GeoMap.my_en, GeoMap.my_ms, GeoMap.nz, GeoMap.th_en, GeoMap.th_th];
// countries where tax is displayed for some segments only by default
const DISPLAY_TAX_MAP = {
'INDIVIDUAL_COM': [GeoMap.za, GeoMap.lt, GeoMap.lv, GeoMap.ng, GeoMap.sa_ar, GeoMap.sa_en, GeoMap.za, GeoMap.sg, GeoMap.kr], // individual
'TEAM_COM': [GeoMap.za, GeoMap.lt, GeoMap.lv, GeoMap.ng, GeoMap.za, GeoMap.co, GeoMap.kr], // business
'INDIVIDUAL_EDU': [GeoMap.lt, GeoMap.lv, GeoMap.sa_en, GeoMap.sea], // student
'TEAM_EDU': [GeoMap.sea, GeoMap.kr], // school and uni
};

/** @type {Commerce.Price.PlaceholderConstructor} */
export class HTMLPriceSpanElement extends HTMLSpanElement {
Expand Down Expand Up @@ -79,6 +93,44 @@ export class HTMLPriceSpanElement extends HTMLSpanElement {
return this;
}

/**
* Resolves default value of displayTax property, based on provided geo info and segments.
* @returns {boolean}
*/
resolveDisplayTaxForGeoAndSegment(country, language, customerSegment, marketSegment) {
const locale = `${country}_${language}`;
if (DISPLAY_ALL_TAX_COUNTRIES.includes(country)
|| DISPLAY_ALL_TAX_COUNTRIES.includes(locale)) {
return true;
}

const segmentConfig = DISPLAY_TAX_MAP[`${customerSegment}_${marketSegment}`];
if (!segmentConfig) {
return false;
}

if (segmentConfig.includes(country) || segmentConfig.includes(locale)) {
return true;
}

return false;
}

/**
* Resolves default value of displayTax property, based on provided geo info and segments extracted from offers object.
* @returns {boolean}
*/
async resolveDisplayTax(service, options) {
const [offerSelectors] = await service.resolveOfferSelectors(options);
const offers = selectOffers(await offerSelectors, options);
if (offers?.length) {
const { country, language } = options;
const offer = offers[0];
const [marketSegment = ''] = offer.marketSegments;
return this.resolveDisplayTaxForGeoAndSegment(country, language, offer.customerSegment, marketSegment);
}
}

/**
* Resolves associated osi via Wcs and renders price offer.
* @param {Record<string, any>} overrides
Expand All @@ -92,6 +144,12 @@ export class HTMLPriceSpanElement extends HTMLSpanElement {
this.placeholder
);
if (!options.wcsOsi.length) return false;

if (!this.placeholder.dataset.displayTax) {
// set default value for displayTax if not set neither in OST nor in price URL
options.displayTax = await this.resolveDisplayTax(service, options) || false;
}

const version = this.placeholder.togglePending(options);
this.innerHTML = '';
const [promise] = service.resolveOfferSelectors(options);
Expand Down
26 changes: 23 additions & 3 deletions commerce/src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ const DEFAULT_LOCALE = 'en_US';

const GeoMap = {
ar: 'AR_es',
be_en: 'BE_en',
be_fr: 'BE_fr',
be_nl: 'BE_nl',
br: 'BR_pt',
ca: 'CA_en',
ch_de: 'CH_de',
ch_fr: 'CH_fr',
ch_it: 'CH_it',
cl: 'CL_es',
co: 'CO_es',
la: 'DO_es',
Expand All @@ -28,13 +34,22 @@ const GeoMap = {
dk: 'DK_da',
de: 'DE_de',
ee: 'EE_et',
eg_ar: 'EG_ar',
eg_en: 'EG_en',
es: 'ES_es',
fr: 'FR_fr',
gr_el: 'GR_el',
gr_en: 'GR_en',
ie: 'IE_en',
il_he: 'IL_iw',
it: 'IT_it',
lv: 'LV_lv',
lt: 'LT_lt',
lu_de: 'LU_de',
lu_en: 'LU_en',
lu_fr: 'LU_fr',
my_en: 'MY_en',
my_ms: 'MY_ms',
hu: 'HU_hu',
mt: 'MT_en',
mena_en: 'DZ_en',
Expand All @@ -56,9 +71,13 @@ const GeoMap = {
ru: 'RU_ru',
ua: 'UA_uk',
au: 'AU_en',
in: 'IN_en',
in_en: 'IN_en',
in_hi: 'IN_hi',
id_en: 'ID_en',
id_id: 'ID_in',
nz: 'NZ_en',
sa_ar: 'SA_ar',
sa_en: 'SA_en',
sg: 'SG_en',
cn: 'CN_zh-Hans',
tw: 'TW_zh-Hant',
Expand All @@ -74,7 +93,8 @@ const GeoMap = {
cis_en: 'AZ_en',
cis_ru: 'AZ_ru',
sea: 'SG_en',
th: 'TH_en',
th_en: 'TH_en',
th_th: 'TH_th',
};

const HostEnv = Object.freeze({
Expand Down Expand Up @@ -211,4 +231,4 @@ function getSettings(config = {}) {
};
}

export { HostEnv as MiloEnv, getLocaleSettings, getSettings };
export { HostEnv as MiloEnv, getLocaleSettings, getSettings, GeoMap };
Loading

0 comments on commit f712f43

Please sign in to comment.