File

projects/storefrontlib/cms-components/cart/cart-details/cart-details.component.ts

Implements

OnInit

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector cx-cart-details
templateUrl ./cart-details.component.html

Index

Properties
Methods

Constructor

constructor(activeCartService: ActiveCartService, selectiveCartService: SelectiveCartService, authService: AuthService, routingService: RoutingService)
Parameters :
Name Type Optional
activeCartService ActiveCartService No
selectiveCartService SelectiveCartService No
authService AuthService No
routingService RoutingService No

Methods

ngOnInit
ngOnInit()
Returns : void
saveForLater
saveForLater(item: OrderEntry)
Parameters :
Name Type Optional
item OrderEntry No
Returns : void

Properties

cart$
Type : Observable<Cart>
cartLoaded$
Type : Observable<boolean>
entries$
Type : Observable<OrderEntry[]>
loggedIn
Default value : false
promotionLocation
Type : PromotionLocation
Default value : PromotionLocation.ActiveCart
selectiveCartEnabled
Type : boolean
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import {
  ActiveCartService,
  AuthService,
  Cart,
  OrderEntry,
  PromotionLocation,
  RoutingService,
  SelectiveCartService,
} from '@spartacus/core';
import { combineLatest, Observable, of } from 'rxjs';
import { filter, map, tap } from 'rxjs/operators';

@Component({
  selector: 'cx-cart-details',
  templateUrl: './cart-details.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CartDetailsComponent implements OnInit {
  cart$: Observable<Cart>;
  entries$: Observable<OrderEntry[]>;
  cartLoaded$: Observable<boolean>;
  loggedIn = false;
  promotionLocation: PromotionLocation = PromotionLocation.ActiveCart;
  selectiveCartEnabled: boolean;

  constructor(
    protected activeCartService: ActiveCartService,
    protected selectiveCartService: SelectiveCartService,
    protected authService: AuthService,
    protected routingService: RoutingService
  ) {}

  ngOnInit() {
    this.cart$ = this.activeCartService.getActive();

    this.entries$ = this.activeCartService
      .getEntries()
      .pipe(filter((entries) => entries.length > 0));

    this.selectiveCartEnabled = this.selectiveCartService.isEnabled();

    this.cartLoaded$ = combineLatest([
      this.activeCartService.isStable(),
      this.selectiveCartEnabled
        ? this.selectiveCartService.isStable()
        : of(false),
      this.authService.isUserLoggedIn(),
    ]).pipe(
      tap(([, , loggedIn]) => (this.loggedIn = loggedIn)),
      map(([cartLoaded, sflLoaded, loggedIn]) =>
        loggedIn && this.selectiveCartEnabled
          ? cartLoaded && sflLoaded
          : cartLoaded
      )
    );
  }

  saveForLater(item: OrderEntry) {
    if (this.loggedIn) {
      this.activeCartService.removeEntry(item);
      this.selectiveCartService.addEntry(item.product.code, item.quantity);
    } else {
      this.routingService.go({ cxRoute: 'login' });
    }
  }
}
<ng-container *ngIf="cart$ | async as cart">
  <ng-container *ngIf="entries$ | async as entries">
    <div *ngIf="cart.totalItems > 0" class="cart-details-wrapper">
      <ng-container *cxFeatureLevel="'4.2'">
        <cx-cart-validation-warnings></cx-cart-validation-warnings>
      </ng-container>

      <h4 class="cx-total">
        {{ 'cartDetails.cartName' | cxTranslate: { code: cart.code } }}
      </h4>

      <cx-promotions
        [promotions]="
          (cart.appliedOrderPromotions || []).concat(
            cart.potentialOrderPromotions || []
          )
        "
      ></cx-promotions>

      <cx-cart-item-list
        [items]="entries"
        [cartIsLoading]="!(cartLoaded$ | async)"
        [promotionLocation]="promotionLocation"
        [options]="{
          isSaveForLater: false,
          optionalBtn: saveForLaterBtn
        }"
      ></cx-cart-item-list>
    </div>
  </ng-container>
</ng-container>

<ng-template let-ctx #saveForLaterBtn>
  <div
    *ngIf="selectiveCartEnabled"
    class="col-md-3 col-lg-3 col-xl-3 cx-sfl-btn"
  >
    <button
      class="link cx-action-link"
      [disabled]="ctx.loading"
      (click)="saveForLater(ctx.item)"
      type="button"
    >
      {{ 'saveForLaterItems.saveForLater' | cxTranslate }}
    </button>
  </div>
</ng-template>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""