Skip to content

Commit

Permalink
Checkout: Do not override coupon with Jetpack default if already appl…
Browse files Browse the repository at this point in the history
…ied (#50662)

* Do not override coupon if already applied

If the cart already has a coupon applied when checkout loads, there's no
need to replace it with the jetpack default coupon.

* Shopping Cart: allow applying a new coupon even if one is applied
  • Loading branch information
sirbrillig authored Mar 2, 2021
1 parent 1657210 commit ad3e6e1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ const wpcomGetStoredCards = (): StoredCard[] => wpcom.getStoredCards();
// Can be safely removed after 2021-03-03 when the FRESHPACK coupon expires
import moment from 'moment';
import { isJetpackProductSlug, isJetpackPlanSlug } from 'calypso/lib/products-values';
const useMaybeGetFRESHPACKCode = ( products: RequestCartProduct[] ) =>
const useMaybeGetFRESHPACKCode = ( products: RequestCartProduct[], isCouponApplied: boolean ) =>
useMemo( () => {
if ( isCouponApplied ) {
return undefined;
}
const includesJetpackItems = products
.map( ( p ) => p.product_slug )
.some( ( slug ) => isJetpackProductSlug( slug ) || isJetpackPlanSlug( slug ) );
Expand All @@ -122,7 +125,7 @@ const useMaybeGetFRESHPACKCode = ( products: RequestCartProduct[] ) =>
const endDate = moment.utc( '2021-03-04' );

return moment().isBefore( endDate ) ? 'FRESHPACK' : undefined;
}, [ products ] );
}, [ products, isCouponApplied ] );

export default function CompositeCheckout( {
siteSlug,
Expand Down Expand Up @@ -245,7 +248,10 @@ export default function CompositeCheckout( {
addProductsToCart,
} = useShoppingCart();

const maybeFRESHPACKCode = useMaybeGetFRESHPACKCode( productsForCart );
const maybeFRESHPACKCode = useMaybeGetFRESHPACKCode(
productsForCart,
couponStatus === 'applied'
);

const isInitialCartLoading = useAddProductsFromUrl( {
isLoadingCart,
Expand Down
5 changes: 4 additions & 1 deletion packages/shopping-cart/src/use-shopping-cart-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ function shoppingCartReducer(

case 'ADD_COUPON': {
const newCoupon = action.couponToAdd;
if ( couponStatus === 'applied' || couponStatus === 'pending' ) {
if (
( couponStatus === 'applied' || couponStatus === 'pending' ) &&
newCoupon === state.responseCart.coupon
) {
debug( `coupon status is '${ couponStatus }'; not submitting again` );
return state;
}
Expand Down

0 comments on commit ad3e6e1

Please sign in to comment.