2019-11-07 18:31:02 +00:00
|
|
|
/** @format */
|
2019-12-10 19:01:21 +00:00
|
|
|
|
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
|
|
|
|
2019-12-10 19:01:21 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
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.
|
|
|
|
*
|
|
|
|
* @format
|
|
|
|
* @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 );
|
|
|
|
const euCountries = without( getSetting( 'onboarding', { euCountries: [] } ).euCountries, 'GB' );
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param {object} profileItems Onboarding profile.
|
|
|
|
* @return {array} Product Ids.
|
|
|
|
*/
|
|
|
|
export function getProductIdsForCart( profileItems ) {
|
|
|
|
const productIds = [];
|
|
|
|
const onboarding = getSetting( 'onboarding', {} );
|
|
|
|
const productTypes = profileItems.product_types || [];
|
|
|
|
|
|
|
|
productTypes.forEach( productType => {
|
|
|
|
if (
|
|
|
|
onboarding.productTypes[ productType ] &&
|
|
|
|
onboarding.productTypes[ productType ].product
|
|
|
|
) {
|
|
|
|
productIds.push( onboarding.productTypes[ productType ].product );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
const theme = onboarding.themes.find( themeData => themeData.slug === profileItems.theme );
|
|
|
|
|
2019-12-30 10:11:50 +00:00
|
|
|
if ( theme && theme.id && ! theme.is_installed && getPriceValue( theme.price ) > 0 ) {
|
2019-12-20 12:58:38 +00:00
|
|
|
productIds.push( theme.id );
|
|
|
|
}
|
|
|
|
|
|
|
|
return productIds;
|
|
|
|
}
|
|
|
|
|
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, '' ) );
|
|
|
|
}
|
|
|
|
|
2019-11-07 18:31:02 +00:00
|
|
|
/**
|
|
|
|
* Returns if the onboarding feature of WooCommerce Admin should be enabled.
|
|
|
|
*
|
|
|
|
* While we preform an a/b test of onboarding, the feature will be enabled within the plugin build,
|
|
|
|
* but only if the user recieved the test/opted in.
|
|
|
|
*
|
|
|
|
* @return {bool} True if the onboarding is enabled.
|
|
|
|
*/
|
|
|
|
export function isOnboardingEnabled() {
|
|
|
|
if ( ! window.wcAdminFeatures.onboarding ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getSetting( 'onboardingEnabled', false );
|
|
|
|
}
|