File size: 1,475 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {
	PLAN_ECOMMERCE_TRIAL_MONTHLY,
	camelOrSnakeSlug,
	isEcommerce,
	isWooExpressPlan,
} from '@automattic/calypso-products';
import { ResponseCart, ResponseCartProduct } from '@automattic/shopping-cart';

/**
 * Checks whether the upgrade product is valid for upsell tracking.
 * There may be more conditions, for now it's just checking whether
 * it's any ecommerce plan.
 * @param product ResponseCartProduct
 * @returns { boolean }
 */
export const isValidWooExpressUpsell = ( product: ResponseCartProduct ): boolean => {
	return isEcommerce( product ) || isWooExpressPlan( camelOrSnakeSlug( product ) );
};

/**
 * Checks if the cart contains an upgrade to a valid plan, and whether the site has a woo trial.
 * If both are true, we'll return true. A valid upgrade assumes that the site currently (pre upgrade)
 * has a Woo Experss trial, and that the upgrade is to a valid ecommerce plan as defined by the return
 * value of `isEcommerce`.
 * @param cart ResponseCart
 * @param sitePlanSlug SiteDetails
 * @returns { boolean }
 */
export const isWooExpressUpgrade = ( cart: ResponseCart, sitePlanSlug?: string ): boolean => {
	// Does the current site have a plan and is it Woo Express trial pre-upgrade?
	if ( ! sitePlanSlug || sitePlanSlug !== PLAN_ECOMMERCE_TRIAL_MONTHLY ) {
		return false;
	}

	// Does the cart contain an upsell we care about? I.e. is it an ecommerce plan?
	return cart.products.some( ( product ) => isValidWooExpressUpsell( product ) );
};