SAP Spartacus 中的 checkout(结帐) 设计
https://sap.github.io/spartacus-docs/extending-checkout
The checkout feature in Spartacus is CMS-driven, which means every page in the checkout flow is based on CMS pages, slots and components.
Spartacus 中的 checkout 功能也是 CMS-driven. checkout flow 中每個頁面都基于 CMS page,slots 和 Components.
Routing and Configuration
In the checkout, you often have links from one step to another, which is the reason for registering each checkout page as a semantic page in the storefront configuration.
在 checkout 流程里,我們通常從一個鏈接跳轉(zhuǎn)到另一個鏈接,因此 Spartacus 采取在 Storefront 配置里,將每個 checkout page 注冊成 semantic page.
默認(rèn)的 checkout 配置:
Although the default checkout has four steps, the default configuration defines five semantic pages.
This additional page has a general checkout route that is linked to from every component that should redirect to the checkout. From this general checkout page, Spartacus redirects to the correct checkout step.
雖然默認(rèn)的 checkout 只有4個步驟,但是配置里包含了5個semantic 頁面,具有一個通用的路由,路徑為 route.
4個步驟對應(yīng)的頁面:
If you want to link to the checkout, always point to this general checkout page, regardless of how your checkout is set up. For example, with a multi-step checkout, you can use your CheckoutGuard to take care of redirecting to the correct checkout step.
使用自己實現(xiàn)的 CheckoutGuard 跳轉(zhuǎn)到正確的 checkout step 上去。
With a single-step checkout, you can set all components on this checkout route and remove the CheckoutGuard from the component configuration.
如果要實現(xiàn)單步 checkout 步驟,從 Component configuration 中移除 CheckoutGuard.
Aside from the route configuration, you can also configure the checkout by defining the responsibility of each step, the route to the page, and the order of the steps. The following is the default configuration:
B2cStorefrontModule.withConfig({checkout: {steps: [{id: 'shippingAddress',name: 'checkoutProgress.shippingAddress',routeName: 'checkoutShippingAddress',type: [CheckoutStepType.SHIPPING_ADDRESS],},{id: 'deliveryMode',name: 'checkoutProgress.deliveryMode',routeName: 'checkoutDeliveryMode',type: [CheckoutStepType.DELIVERY_MODE],},{id: 'paymentDetails',name: 'checkoutProgress.paymentDetails',routeName: 'checkoutPaymentDetails',type: [CheckoutStepType.PAYMENT_DETAILS],},{id: 'reviewOrder',name: 'checkoutProgress.reviewOrder',routeName: 'checkoutReviewOrder',type: [CheckoutStepType.REVIEW_ORDER],},],}, })- id:checkout step 的唯一標(biāo)識
- name:used in the CheckoutProgress component to indicate which checkout steps have been completed.
用于 CheckoutProgress 中,標(biāo)識當(dāng)前已經(jīng)完成了哪一個 checkout 步驟。
- routeName:specifies the semantic page for each step - 每個 checkout 步驟的 semantic 頁面名稱
- type:用于 checkout guards
steps 數(shù)組里的步驟決定了 checkout 次序。
Every checkout component is a CMS component.
每個 checkout Component 都是 CMS Component.
Furthermore, in the default checkout, all components are CMSFlexComponents.
進一步的說,每一個 Component 都是 CMSFlexComponents.
For Angular or web components that do not need any data from CMS (for example, login), you can use the CMS component of type CMSFlexComponent as a placeholder.
對于那些不需要來自 CMS 數(shù)據(jù)的 Angular 或者 Web Component,我們可以使用 類型為 CMSFlexComponent 的 CMS Component,作為 placeholder.這種類型的 Component,具有特殊的 flexType 屬性,在 Spartacus CMS mapping 中,flexType 屬性被使用。
these components have more guards defined in the configuration, but are otherwise identical to regular CMS components.
CMSFlexComponent 在配置里具有更多的 guards,除此之外,同普通的 CMS Component 沒有什么區(qū)別。
The checkout uses component guards instead of page guards. You protect routes by applying guards in the CMS component mapping.
- checkout 步驟使用 Component guard,而不是 page guard.
- 在 CMS Component mapping 里定義 guard.
Spartacus 提供了下列的 Component guard:
(1) ShippingAddressSetGuard
(2) DeliveryModeSetGuard
(3) PaymentDetailsSetGuard
(4) CheckoutGuard
As an example, if you wanted to restrict access to the Review Order page, so that it displays only when the shipping address, delivery mode and payment details were correctly set, you would set guards for the review order component to guards: [ShippingAddressSetGuard, DeliveryModeSetGuard, PaymentDetailsSetGuard].
例如,假設(shè)需求是 Review Order Page 只有在前三個步驟都通過的時候才能打開,則給 Review order Component 設(shè)置如下的 guard:
[ShippingAddressSetGuard, DeliveryModeSetGuard, PaymentDetailsSetGuard]when you try to access the Review Order page, Spartacus first checks the guards for every component on that page, and only displays the page if every guard returns true. If one of the guard returns false, or returns a redirect URL, Spartacus redirects to the provided URL.
因此,當(dāng)試圖訪問 Review Order 頁面時,Spartacus 首先遍歷這個頁面所有的 Component guards,只有這些 guard 全部返回 true 之后,才能打開 Review order 頁面。
The order of the guards is important because the first redirect is used. In general, you should define guards in the same order as the checkout flow.
Component guard 最先返回哪個 redirect url,用戶就被重定向到哪個 url 去。所以 Component guard 的順序很重要。
如何定義重定向 url
In the checkout configuration, for each step, you specify a type attribute and the type of data that should be set. A guard looks for the first step that contains the specific type and then redirects to this step.
For example, ShippingAddressSetGuard searches the checkout configuration for the first step with a type containing CheckoutStepType.shippingAddress, then reads the step route and redirects to that page.
CMS Catalog Content and Default Template
點擊購物車按鈕后,跳轉(zhuǎn)到 checkout route:
當(dāng)從 cart 跳轉(zhuǎn)到 checkout 時,有一個 cancel 操作,cancel 的是 checkout default step:
然后發(fā)起向 checkout/shipping-address 的請求:
最終跳轉(zhuǎn)到 shipping address url:
http://localhost:4200/electronics-spa/en/USD/checkout
In the default checkout, Spartacus uses a modified MultiStepCheckoutOrderSummaryPageTemplate.
如下圖所示:
包含 16個 content slot:
調(diào)換 checkout 步驟的順序
一個例子:
The following scenario describes how to change the order of two steps. In the default configuration, the checkout flow starts with setting a shipping address, followed by setting the delivery mode, and finally by filling in the payment details. In this scenario, the configuration is modified so that the payment details step occurs before the delivery mode step.
@NgModule({imports: [B2cStorefrontModule.withConfig({...restOfConfig,checkout: {// You must specify all of the steps (this configuration is not merged with the default one)steps: [{id: 'shippingAddress',name: 'checkoutProgress.shippingAddress',routeName: 'checkoutShippingAddress',type: [CheckoutStepType.SHIPPING_ADDRESS],},// Change the payment details step to be before the delivery mode{id: 'paymentDetails',name: 'checkoutProgress.paymentDetails',routeName: 'checkoutPaymentDetails',type: [CheckoutStepType.PAYMENT_DETAILS],},{id: 'deliveryMode',name: 'checkoutProgress.deliveryMode',routeName: 'checkoutDeliveryMode',type: [CheckoutStepType.DELIVERY_MODE],},{id: 'reviewOrder',name: 'checkoutProgress.reviewOrder',routeName: 'checkoutReviewOrder',type: [CheckoutStepType.REVIEW_ORDER],},],},cmsComponents: {CheckoutPaymentDetails: {component: PaymentMethodsComponent,// The default CheckoutPaymentDetails uses the DeliveryModeSetGuard, but in this case, the delivery mode details will not be set yet.// Instead, override the component guards with a new set that does not include the DeliveryModeSetGuardguards: [AuthGuard, CartNotEmptyGuard, ShippingAddressSetGuard],},CheckoutDeliveryMode: {component: DeliveryModeComponent,// In the CheckoutDeliveryMode, we need to also check if the payment details are set, so we add the PaymentDetailsSetGuardguards: [AuthGuard,CartNotEmptyGuard,ShippingAddressSetGuard,PaymentDetailsSetGuard,],},},}),],bootstrap: [StorefrontComponent], }) export class AppModule {}CheckoutPaymentDetails Component,只有在進入 payment step 時才加載:
CheckoutPaymentDetails
在 CMS Component mapping 里指定 Component guard:
更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:
總結(jié)
以上是生活随笔為你收集整理的SAP Spartacus 中的 checkout(结帐) 设计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 普林斯顿体系架构和哈佛架构的区别_边和
- 下一篇: qt设置程序图标_qt设置软件图标