Skip to content

Commit abbf6ad

Browse files
committed
Update the product tier logic to use the new format
1 parent 0f73351 commit abbf6ad

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

client/a8c-for-agencies/sections/marketplace/hooks/use-marketplace.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,28 @@ export const calculateDiscountPercentage = (
3535
export const getWPCOMTieredPrice = (
3636
product: APIProductFamilyProduct,
3737
quantity: number,
38-
termPricing: TermPricingType
38+
termPricing: TermPricingType,
39+
ownedPlans = 0
3940
) => {
4041
// Calculate actual cost (base product price * quantity)
4142
const basePricePerUnit =
4243
termPricing === 'yearly' ? product.yearly_price ?? 0 : product.monthly_price ?? 0;
4344
const actualCost = basePricePerUnit * quantity;
45+
const tierPrices =
46+
termPricing === 'yearly' ? product.tier_yearly_prices : product.tier_monthly_prices;
47+
const tierQuantity = quantity + ownedPlans;
4448

45-
// Find tier for exact quantity, or for 10 units if quantity > 10
49+
// Find tier for exact quantity, or get the greatest unit that is less than or equal to tierQuantity
4650
const tier =
47-
product?.price_tier_list?.find( ( t ) => t.units === quantity ) ||
48-
( quantity > 10 ? product?.price_tier_list?.find( ( t ) => t.units === 10 ) : undefined );
51+
tierPrices?.find( ( t ) => t.units === tierQuantity ) ||
52+
tierPrices
53+
?.filter( ( t ) => t.units <= tierQuantity )
54+
.sort( ( a, b ) => b.units - a.units )[ 0 ];
4955

5056
// Get price per unit from tier if found, otherwise from product
5157
let pricePerUnit: number;
5258
if ( tier ) {
53-
pricePerUnit = termPricing === 'yearly' ? tier.yearly_price : tier.monthly_price;
59+
pricePerUnit = tier.price;
5460
} else {
5561
pricePerUnit = basePricePerUnit;
5662
}
@@ -200,7 +206,7 @@ export const useGetProductPricingInfo = (
200206
let discountPercentage: number;
201207

202208
if ( isTermPricingEnabled && termPricing ) {
203-
const pricingInfo = getWPCOMTieredPrice( product, quantity, termPricing );
209+
const pricingInfo = getWPCOMTieredPrice( product, quantity, termPricing, ownedPlans );
204210
actualCost = pricingInfo.actualCost;
205211
discountedCost = pricingInfo.discountedCost;
206212
discountPercentage = pricingInfo.discountPercentage;

client/a8c-for-agencies/sections/marketplace/hosting-overview/hosting-content/standard-agency-hosting/wpcom-plan-section/wpcom-plan-selector.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export default function WPCOMPlanSelector( {
6464

6565
const originalPrice = Number( plan?.amount ?? 0 ) * quantity;
6666
const pricingInfo = isTermPricingEnabled
67-
? getWPCOMTieredPrice( plan, quantity, termPricing )
67+
? getWPCOMTieredPrice( plan, quantity, termPricing, ownedPlans )
6868
: {
6969
actualCost: originalPrice,
7070
discountedCost: originalPrice - originalPrice * discount,

client/a8c-for-agencies/sections/marketplace/hosting-overview/hosting-content/standard-agency-hosting/wpcom-plan-selector/slider.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ const createOptionsFromPriceTierList = (
2323
plan: APIProductFamilyProduct,
2424
termPricing: TermPricingType
2525
) => {
26-
const priceTierList = plan.price_tier_list || [];
26+
const priceTierList =
27+
termPricing === 'yearly' ? plan.tier_yearly_prices || [] : plan.tier_monthly_prices || [];
2728
const basePrice = termPricing === 'yearly' ? plan.yearly_price ?? 0 : plan.monthly_price ?? 0;
2829

2930
const options = priceTierList.map( ( tier ) => {
30-
const tierPrice = termPricing === 'yearly' ? tier.yearly_price : tier.monthly_price;
31-
const discountPercent = calculateDiscountPercentage( basePrice, tierPrice );
31+
const discountPercent = calculateDiscountPercentage( basePrice, tier.price );
3232

3333
return {
3434
value: tier.units,
@@ -69,7 +69,7 @@ export default function WPCOMPlanSlider( { quantity, ownedPlans, onChange, plan
6969
let sliderOptions = [];
7070

7171
if ( isTermPricingEnabled && termPricing && plan ) {
72-
// Use price_tier_list when term pricing is enabled
72+
// Use tiered prices when term pricing is enabled
7373
sliderOptions = createOptionsFromPriceTierList( plan, termPricing );
7474
} else {
7575
// Fallback to discount tiers

0 commit comments

Comments
 (0)