2019-11-07 18:31:02 +00:00
|
|
|
/**
|
2019-12-10 19:01:21 +00:00
|
|
|
* External dependencies
|
2019-11-07 18:31:02 +00:00
|
|
|
*/
|
2019-12-30 10:11:50 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2019-12-10 19:01:21 +00:00
|
|
|
import { without } from 'lodash';
|
2019-11-07 18:31:02 +00:00
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
|
|
|
|
2019-08-26 05:49:04 +00:00
|
|
|
/**
|
|
|
|
* Gets the country code from a country:state value string.
|
|
|
|
*
|
|
|
|
* @param {string} countryState Country state string, e.g. US:GA.
|
|
|
|
* @return {string} Country string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function getCountryCode( countryState ) {
|
|
|
|
if ( ! countryState ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return countryState.split( ':' )[ 0 ];
|
|
|
|
}
|
2019-11-07 18:31:02 +00:00
|
|
|
|
2019-12-10 19:01:21 +00:00
|
|
|
export function getCurrencyRegion( countryState ) {
|
|
|
|
let region = getCountryCode( countryState );
|
2020-02-14 02:23:21 +00:00
|
|
|
const euCountries = without(
|
|
|
|
getSetting( 'onboarding', { euCountries: [] } ).euCountries,
|
|
|
|
'GB'
|
|
|
|
);
|
2019-12-10 19:01:21 +00:00
|
|
|
if ( euCountries.includes( region ) ) {
|
|
|
|
region = 'EU';
|
|
|
|
}
|
|
|
|
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:58:38 +00:00
|
|
|
/**
|
|
|
|
* Gets the product IDs for items based on the product types and theme selected in the onboarding profiler.
|
|
|
|
*
|
2021-10-13 16:15:47 +00:00
|
|
|
* @param {Object} productTypes Product Types.
|
2020-02-14 02:23:21 +00:00
|
|
|
* @param {Object} profileItems Onboarding profile.
|
|
|
|
* @param {boolean} includeInstalledItems Include installed items in returned product IDs.
|
2020-05-25 00:26:08 +00:00
|
|
|
* @param {Array} installedPlugins Installed plugins.
|
2020-02-14 02:23:21 +00:00
|
|
|
* @return {Array} Product Ids.
|
2019-12-20 12:58:38 +00:00
|
|
|
*/
|
2020-02-14 02:23:21 +00:00
|
|
|
export function getProductIdsForCart(
|
2021-10-13 16:15:47 +00:00
|
|
|
productTypes,
|
2020-02-14 02:23:21 +00:00
|
|
|
profileItems,
|
2020-05-25 00:26:08 +00:00
|
|
|
includeInstalledItems = false,
|
|
|
|
installedPlugins
|
2020-08-03 19:24:57 +00:00
|
|
|
) {
|
|
|
|
const productList = getProductList(
|
|
|
|
profileItems,
|
|
|
|
includeInstalledItems,
|
2021-10-13 16:15:47 +00:00
|
|
|
installedPlugins,
|
|
|
|
productTypes
|
2020-08-03 19:24:57 +00:00
|
|
|
);
|
|
|
|
const productIds = productList.map(
|
|
|
|
( product ) => product.id || product.product
|
|
|
|
);
|
|
|
|
return productIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the labeled/categorized product names and types for items based on the product types and theme selected in the onboarding profiler.
|
|
|
|
*
|
2021-10-13 16:15:47 +00:00
|
|
|
* @param {Object} productTypes Product Types.
|
2020-08-03 19:24:57 +00:00
|
|
|
* @param {Object} profileItems Onboarding profile.
|
|
|
|
* @param {Array} installedPlugins Installed plugins.
|
|
|
|
* @return {Array} Objects with labeled/categorized product names and types.
|
|
|
|
*/
|
|
|
|
export function getCategorizedOnboardingProducts(
|
2021-10-13 16:15:47 +00:00
|
|
|
productTypes,
|
2020-08-03 19:24:57 +00:00
|
|
|
profileItems,
|
|
|
|
installedPlugins
|
|
|
|
) {
|
|
|
|
const productList = {};
|
|
|
|
productList.products = getProductList(
|
|
|
|
profileItems,
|
|
|
|
true,
|
2021-10-13 16:15:47 +00:00
|
|
|
installedPlugins,
|
|
|
|
productTypes
|
2020-08-03 19:24:57 +00:00
|
|
|
);
|
|
|
|
productList.remainingProducts = getProductList(
|
|
|
|
profileItems,
|
|
|
|
false,
|
2021-10-13 16:15:47 +00:00
|
|
|
installedPlugins,
|
|
|
|
productTypes
|
2020-08-03 19:24:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const uniqueItemsList = [
|
|
|
|
...new Set( [
|
|
|
|
...productList.products,
|
|
|
|
...productList.remainingProducts,
|
|
|
|
] ),
|
|
|
|
];
|
|
|
|
|
|
|
|
productList.uniqueItemsList = uniqueItemsList.map( ( product ) => {
|
|
|
|
let cleanedProduct;
|
|
|
|
if ( product.label ) {
|
|
|
|
cleanedProduct = { type: 'extension', name: product.label };
|
|
|
|
} else {
|
|
|
|
cleanedProduct = { type: 'theme', name: product.title };
|
|
|
|
}
|
|
|
|
return cleanedProduct;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return productList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a product list for items based on the product types and theme selected in the onboarding profiler.
|
|
|
|
*
|
|
|
|
* @param {Object} profileItems Onboarding profile.
|
|
|
|
* @param {boolean} includeInstalledItems Include installed items in returned product list.
|
|
|
|
* @param {Array} installedPlugins Installed plugins.
|
2021-10-13 16:15:47 +00:00
|
|
|
* @param {Object} productTypes Product Types.
|
2020-08-03 19:24:57 +00:00
|
|
|
* @return {Array} Products.
|
|
|
|
*/
|
|
|
|
export function getProductList(
|
|
|
|
profileItems,
|
|
|
|
includeInstalledItems = false,
|
2021-10-13 16:15:47 +00:00
|
|
|
installedPlugins,
|
|
|
|
productTypes
|
2020-02-14 02:23:21 +00:00
|
|
|
) {
|
2020-08-03 19:24:57 +00:00
|
|
|
const productList = [];
|
2019-12-20 12:58:38 +00:00
|
|
|
|
2021-10-13 16:15:47 +00:00
|
|
|
if ( ! productTypes ) {
|
2020-08-03 19:24:57 +00:00
|
|
|
return productList;
|
2020-01-06 23:10:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 16:15:47 +00:00
|
|
|
const profileItemsProductTypes = profileItems.product_types || [];
|
2020-05-13 00:29:41 +00:00
|
|
|
|
2021-10-13 16:15:47 +00:00
|
|
|
profileItemsProductTypes.forEach( ( productType ) => {
|
2019-12-20 12:58:38 +00:00
|
|
|
if (
|
2021-10-13 16:15:47 +00:00
|
|
|
productTypes[ productType ] &&
|
|
|
|
productTypes[ productType ].product &&
|
2019-12-31 08:50:45 +00:00
|
|
|
( includeInstalledItems ||
|
2020-05-25 00:26:08 +00:00
|
|
|
! installedPlugins.includes(
|
2021-10-13 16:15:47 +00:00
|
|
|
productTypes[ productType ].slug
|
2020-02-14 02:23:21 +00:00
|
|
|
) )
|
2019-12-20 12:58:38 +00:00
|
|
|
) {
|
2021-10-13 16:15:47 +00:00
|
|
|
productList.push( productTypes[ productType ] );
|
2019-12-20 12:58:38 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2021-10-13 16:15:47 +00:00
|
|
|
const onboarding = getSetting( 'onboarding', {} );
|
2021-10-19 18:00:54 +00:00
|
|
|
let theme = null;
|
|
|
|
if ( onboarding && onboarding.themes ) {
|
|
|
|
theme = onboarding.themes.find(
|
|
|
|
( themeData ) => themeData.slug === profileItems.theme
|
|
|
|
);
|
|
|
|
}
|
2019-12-20 12:58:38 +00:00
|
|
|
|
2019-12-31 08:50:45 +00:00
|
|
|
if (
|
|
|
|
theme &&
|
|
|
|
theme.id &&
|
|
|
|
getPriceValue( theme.price ) > 0 &&
|
|
|
|
( includeInstalledItems || ! theme.is_installed )
|
|
|
|
) {
|
2020-08-03 19:24:57 +00:00
|
|
|
productList.push( theme );
|
2019-12-20 12:58:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 19:24:57 +00:00
|
|
|
return productList;
|
2019-12-20 12:58:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 10:11:50 +00:00
|
|
|
/**
|
|
|
|
* Get the value of a price from a string, removing any non-numeric characters.
|
|
|
|
*
|
|
|
|
* @param {string} string Price string.
|
|
|
|
* @return {number} Number value.
|
|
|
|
*/
|
|
|
|
export function getPriceValue( string ) {
|
|
|
|
return Number( decodeEntities( string ).replace( /[^0-9.-]+/g, '' ) );
|
|
|
|
}
|
|
|
|
|
2020-08-05 22:02:24 +00:00
|
|
|
/**
|
|
|
|
* Determines if a URL is a WC admin url.
|
|
|
|
*
|
|
|
|
* @param {*} url - the url to test
|
|
|
|
* @return {boolean} true if the url is a wc-admin URL
|
|
|
|
*/
|
|
|
|
export function isWCAdmin( url ) {
|
|
|
|
return /admin.php\?page=wc-admin/.test( url );
|
|
|
|
}
|