我们都知道 Magento2 的支付流程包含两步:运送信息以及支付和核验两个步骤。对于个人而言,你可以希望添加额外的逻辑,像是把支付和核验分为两个单独的步骤或者新增自定义的一个独有的步骤。
本文将会带领你在 Magento2 中创建一个新的结账步骤。这步就做一个事情——让用户知道他们是否登陆。这可能不太契合真实情况,但稍后,我们也会介绍更深入的主题。点击了解更多
在支付页面添加新的一步
- 
第一步,创建实现视图模型 的 js 文件
 - 
第二部,为组件创建一个 html 文件
 - 
第三步,在支付页面的布局中添加新的步骤
 
你应该知道如何创建一个基本的 Magento2 模块。所有的自定义文件都在这个模块中。
让我们开始吧。
第一步,创建实现视图模型 的 js 文件
在 Mageplaza/HelloWorld/view/frontend/web/js/view 目录下创建 checkout-login-step.js 文件。首先,我们需要 step_code, step_title, order 和 允许这个步骤显示的 condition 。
这里是代码(阅读注释可获取更多信息)
define(
    [
        'ko',
        'uiComponent',
        'underscore',
        'Magento_Checkout/js/model/step-navigator',
        'Magento_Customer/js/model/customer'
    ],
    function (
        ko,
        Component,
        _,
        stepNavigator,
        customer
    ) {
        'use strict';
        /**
        * check-login - is the name of the component's .html template
        */
        return Component.extend({
            defaults: {
                template: 'Mageplaza_HelloWorld/check-login'
            },
            //add here your logic to display step,
            isVisible: ko.observable(true),
            isLogedIn: customer.isLoggedIn(),
            //step code will be used as step content id in the component template
            stepCode: 'isLogedCheck',
            //step title value
            stepTitle: 'Logging Status',
            /**
            *
            * @returns {*}
            */
            initialize: function () {
                this._super();
                // register your step
                stepNavigator.registerStep(
                    this.stepCode,
                    //step alias
                    null,
                    this.stepTitle,
                    //observable property with logic when display step or hide step
                    this.isVisible,
                    _.bind(this.navigate, this),
                    /**
                    * sort order value
                    * 'sort order value' < 10: step displays before shipping step;
                    * 10 < 'sort order value' < 20 : step displays between shipping and payment step
                    * 'sort order value' > 20 : step displays after payment step
                    */
                    15
                );
                return this;
            },
            /**
            * The navigate() method is responsible for navigation between checkout step
            * during checkout. You can add custom logic, for example some conditions
            * for switching to your custom step
            */
            navigate: function () {
            },
            /**
            * @returns void
            */
            navigateToNextStep: function () {
                stepNavigator.next();
            }
        });
    }
);
第二步,为组件创建一个 html 模板
在上面步骤中,我们使用 Mageplaza_HelloWorld/check-login 作为我们的模板,所以我们需要创建它。在 Mageplaza/HelloWorld/view/frontend/web/template 目录下新建一个 check-login.html 文件。
这是代码
<!--Use 'stepCode' as id attribute-->
<li data-bind="fadeVisible: isVisible, attr: { id: stepCode }">
<div class="step-title" data-bind="i18n: stepTitle" data-role="title"></div>
    <div id="checkout-step-title"
         class="step-content"
         data-role="content">
         <p>The customer is <span data-bind="if: !isLogedIn">not</span> Logged-in</p>
        <form data-bind="submit: navigateToNextStep" novalidate="novalidate">
            <div class="actions-toolbar">
                <div class="primary">
                    <button data-role="opc-continue" type="submit" class="button action continue primary">
                        <span><!-- ko i18n: 'Next'--><!-- /ko --></span>
                    </button>
                </div>
            </div>
        </form>
    </div>
</li>
第三步,在支付页面的布局中添加一个新步骤
我们需要扩展结账页面的布局来展示新的步骤。在我们的模块中添加这个文件:Mageplaza/HelloWorld/view/frontend/layout/checkout_index_index.xml
内容如下:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.root">
                <arguments>
                    <argument name="jsLayout" xsi:type="array">
                        <item name="components" xsi:type="array">
                            <item name="checkout" xsi:type="array">
                                <item name="children" xsi:type="array">
                                    <item name="steps" xsi:type="array">
                                        <item name="children" xsi:type="array">
                                            <!-- The new step you add -->
                                            <item name="check-login-step" xsi:type="array">
                                                <item name="component" xsi:type="string">Mageplaza_HelloWorld/js/view/checkout-login-step</item>
                                                    <!--To display step content before shipping step "sortOrder" value should be < 1-->
                                                    <!--To display step content between shipping step and payment step  1 < "sortOrder" < 2 -->
                                                    <!--To display step content after payment step "sortOrder" > 2 -->
                                                <item name="sortOrder" xsi:type="string">2</item>
                                                <item name="children" xsi:type="array">
                                                    <!--add here child component declaration for your step-->
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </argument>
                </arguments>
        </referenceBlock>
    </body>
</page>
这个新的步骤添加到结账页面。清除缓存然后刷新浏览器,结果如下所示:
