Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect users to /domains/add when cart is cleared in EmailUpsell flow. #98519

Merged
merged 6 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions client/layout/masterbar/checkout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ const CheckoutMasterbar = ( {
const { responseCart, replaceProductsInCart } = useShoppingCart( cartKey );
const [ isModalVisible, setIsModalVisible ] = useState( false );

const closeAndLeave = () =>
const closeAndLeave = ( options?: { userHasClearedCart?: boolean } ) =>
leaveCheckout( {
siteSlug,
forceCheckoutBackUrl,
previousPath,
tracksEvent: 'calypso_masterbar_close_clicked',
userHasClearedCart: options?.userHasClearedCart ?? false,
} );

const clickClose = () => {
Expand All @@ -78,7 +79,9 @@ const CheckoutMasterbar = ( {
const modalSecondaryText = translate( 'Empty cart' );
const clearCartAndLeave = () => {
replaceProductsInCart( [] );
closeAndLeave();
closeAndLeave( {
userHasClearedCart: true,
} );
};

const showCloseButton = isLeavingAllowed && checkoutType === 'wpcom';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default function useRemoveFromCartAndRedirect(
createUserAndSiteBeforeTransaction,
previousPath: customizedPreviousPath || previousPath,
tracksEvent: 'calypso_empty_cart_redirect',
userHasClearedCart: true,
} );
}, [
createUserAndSiteBeforeTransaction,
Expand Down
40 changes: 30 additions & 10 deletions client/my-sites/checkout/src/lib/leave-checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,47 @@ import { sendMessageToOpener } from './popup';

const debug = debugFactory( 'calypso:leave-checkout' );

const getCloseURL = ( {
userHasClearedCart,
previousPath,
siteSlug,
}: {
userHasClearedCart: boolean;
previousPath?: string;
siteSlug?: string;
} ): string => {
if (
previousPath &&
previousPath !== '' &&
previousPath !== window.location.href &&
! previousPath.includes( '/checkout/' )
) {
/* Regex to match /domains/add/abc123/email/def456? */
const emailUpsellRegex = /\/domains\/add\/[^/]+\/email\/[^/]+(\?|\/|$)/;

if ( userHasClearedCart && emailUpsellRegex.test( previousPath ) ) {
return '/domains/add/' + siteSlug;
}
return previousPath;
}

return siteSlug ? '/plans/' + siteSlug : '/start';
};

export const leaveCheckout = ( {
siteSlug,
forceCheckoutBackUrl,
previousPath,
tracksEvent,
createUserAndSiteBeforeTransaction,
userHasClearedCart = false,
}: {
siteSlug?: string;
forceCheckoutBackUrl?: string;
previousPath?: string;
tracksEvent: string;
createUserAndSiteBeforeTransaction?: boolean;
userHasClearedCart?: boolean;
} ): void => {
recordTracksEvent( tracksEvent );
debug( 'leaving checkout with args', {
Expand Down Expand Up @@ -68,16 +97,7 @@ export const leaveCheckout = ( {
return;
}

let closeUrl = siteSlug ? '/plans/' + siteSlug : '/start';

if (
previousPath &&
'' !== previousPath &&
previousPath !== window.location.href &&
! previousPath.includes( '/checkout/' )
) {
closeUrl = previousPath;
}
const closeUrl = getCloseURL( { userHasClearedCart, previousPath, siteSlug } );

try {
const searchParams = new URLSearchParams( window.location.search );
Expand Down
63 changes: 63 additions & 0 deletions client/my-sites/checkout/src/test/leave-checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,69 @@ describe( 'leaveCheckout', () => {
expect( navigate ).not.toHaveBeenCalledWith( '/\\example.com' );
} );
} );

describe( 'getCloseURL handling', () => {
it( 'returns to plans page when previous path is empty', () => {
const siteSlug = 'mywpsite.wordpress.com';

leaveCheckout( {
siteSlug: siteSlug,
tracksEvent: 'checkout_cancel',
userHasClearedCart: true,
} );

expect( navigate ).toHaveBeenCalledWith( `/plans/${ siteSlug }` );
} );

it( 'returns to /start if missing site slug', () => {
leaveCheckout( {
tracksEvent: 'checkout_cancel',
userHasClearedCart: true,
} );

expect( navigate ).toHaveBeenCalledWith( '/start' );
} );

it( 'returns to domain page when previous page is email upsell and cart is emptied', () => {
const siteSlug = 'mywpsite.wordpress.com';

leaveCheckout( {
siteSlug: siteSlug,
tracksEvent: 'checkout_cancel',
previousPath: '/domains/add/my-search-domain/email/mywpsite.wordpress.com?',
userHasClearedCart: true,
} );

expect( navigate ).toHaveBeenCalledWith( `/domains/add/${ siteSlug }` );
} );

it( 'returns to previousPath', () => {
const previousPath = `/previous-path`;

leaveCheckout( {
tracksEvent: 'checkout_cancel',
previousPath: previousPath,
userHasClearedCart: false,
} );

expect( navigate ).toHaveBeenCalledWith( previousPath );
} );

it( 'returns to previousPath if email upsell and cart is not emptied', () => {
const siteSlug = 'mywpsite.wordpress.com';
const domain = 'my-search-domain';
const previousPath = `/domains/add/${ domain }/email/${ siteSlug }?`;

leaveCheckout( {
siteSlug: siteSlug,
tracksEvent: 'checkout_cancel',
previousPath: previousPath,
userHasClearedCart: false,
} );

expect( navigate ).toHaveBeenCalledWith( previousPath );
} );
} );
} );

describe( 'isRelativeUrl', () => {
Expand Down
Loading