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

feat: pre-fill delivery address for guest users #17561

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ <h2 class="cx-checkout-title d-none d-lg-block d-xl-block">
<cx-address-form
[showTitleCode]="true"
[setAsDefaultField]="!isGuestCheckout"
[addressData]="selectedAddress"
cancelBtnLabel="{{ backBtnText | cxTranslate }}"
(backToAddress)="hideNewAddressForm(true)"
(submitAddress)="addAddress($event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import {
ChangeDetectionStrategy,
Component,
OnInit,
Optional,
} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ActiveCartFacade } from '@spartacus/cart/base/root';
import {
Expand All @@ -29,6 +34,7 @@ import {
tap,
} from 'rxjs/operators';
import { CheckoutStepService } from '../services/checkout-step.service';
import { CheckoutConfigService } from '../services';

export interface CardWithAddress {
card: Card;
Expand All @@ -49,6 +55,8 @@ export class CheckoutDeliveryAddressComponent implements OnInit {
addressFormOpened = false;
doneAutoSelect = false;

selectedAddress?: Address;

get isGuestCheckout(): boolean {
return !!getLastValueSync(this.activeCartFacade.isGuestCart());
}
Expand All @@ -65,6 +73,32 @@ export class CheckoutDeliveryAddressComponent implements OnInit {
);
}

// TODO(CXSPA-): make checkoutConfigService a required dependency
constructor(
userAddressService: UserAddressService,
checkoutDeliveryAddressFacade: CheckoutDeliveryAddressFacade,
activatedRoute: ActivatedRoute,
translationService: TranslationService,
activeCartFacade: ActiveCartFacade,
checkoutStepService: CheckoutStepService,
checkoutDeliveryModesFacade: CheckoutDeliveryModesFacade,
globalMessageService: GlobalMessageService,
// eslint-disable-next-line @typescript-eslint/unified-signatures
checkoutConfigService: CheckoutConfigService
);
/**
* @deprecated since 6.2
*/
constructor(
userAddressService: UserAddressService,
checkoutDeliveryAddressFacade: CheckoutDeliveryAddressFacade,
activatedRoute: ActivatedRoute,
translationService: TranslationService,
activeCartFacade: ActiveCartFacade,
checkoutStepService: CheckoutStepService,
checkoutDeliveryModesFacade: CheckoutDeliveryModesFacade,
globalMessageService: GlobalMessageService
);
constructor(
protected userAddressService: UserAddressService,
protected checkoutDeliveryAddressFacade: CheckoutDeliveryAddressFacade,
Expand All @@ -73,7 +107,8 @@ export class CheckoutDeliveryAddressComponent implements OnInit {
protected activeCartFacade: ActiveCartFacade,
protected checkoutStepService: CheckoutStepService,
protected checkoutDeliveryModesFacade: CheckoutDeliveryModesFacade,
protected globalMessageService: GlobalMessageService
protected globalMessageService: GlobalMessageService,
@Optional() protected checkoutConfigService?: CheckoutConfigService
) {}

ngOnInit(): void {
Expand Down Expand Up @@ -137,6 +172,14 @@ export class CheckoutDeliveryAddressComponent implements OnInit {
}

addAddress(address: Address | undefined): void {
if (
!address &&
this.shouldUseAddressSavedInCart() &&
this.selectedAddress
) {
this.next();
}

if (!address) {
return;
}
Expand Down Expand Up @@ -242,6 +285,8 @@ export class CheckoutDeliveryAddressComponent implements OnInit {
this.setAddress(selected);
}
this.doneAutoSelect = true;
} else if (selected && this.shouldUseAddressSavedInCart()) {
this.selectedAddress = selected;
}
}

Expand Down Expand Up @@ -296,4 +341,8 @@ export class CheckoutDeliveryAddressComponent implements OnInit {
protected onError(): void {
this.busy$.next(false);
}

protected shouldUseAddressSavedInCart(): boolean {
return !!this.checkoutConfigService?.shouldUseAddressSavedInCart();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export class CheckoutConfigService {
: this.findMatchingDeliveryMode(deliveryModes, index + 1);
}

shouldUseAddressSavedInCart(): boolean {
return !!this.checkoutConfig?.checkout?.guestUseSavedAddress;
}

getPreferredDeliveryMode(deliveryModes: DeliveryMode[]): string | undefined {
deliveryModes.sort(this.compareDeliveryCost);
return this.findMatchingDeliveryMode(deliveryModes);
Expand Down
4 changes: 4 additions & 0 deletions feature-libs/checkout/base/root/config/checkout-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export abstract class CheckoutConfig {
* Allow for guest checkout.
*/
guest?: boolean;
/**
* Use delivery address saved in cart for pre-filling delivery address form.
*/
guestUseSavedAddress?: boolean;
};
}

Expand Down