Magento 2自定义结帐步骤导航

时间:2016-11-15 06:18:08

标签: javascript navigation checkout magento2

我通过在送货和付款步骤之间添加自定义步骤以及在送货步骤中添加复选框来自定​​义magento 2中的结帐流程。

问题

首次进入结帐页面时,我的“送货”和“自定义”步骤均已选中,如图所示。

http://imgur.com/yvVhYZZ

所以有两个问题:

  1. 当我第一次去结帐页面时,我怎么能只看到送货步骤?
  2. 如果我选择我在“运输”步骤中添加的复选框,我可以如何做到这一点,我跳过我的自定义步骤(直接从运费到付款)但如果我没有选中复选框,我会转到我的自定义步骤?
  3. 到目前为止我做了什么

    我已使用magento团队在http://devdocs.magento.com/guides/v2.1/howdoi/checkout/checkout_new_step.html提供的文档创建了自定义步骤

    这是我的 Vendor_CustomCheckout / view / frontend / web / js / view / step-view.js

    define(
    [
        'ko',
        'uiComponent',
        'underscore',
        'Magento_Checkout/js/model/step-navigator',
        'Magento_Ui/js/form/form'
    ],
    function (
        ko,
        Component,
    _,
    stepNavigator
    ) {
        'use strict';
        /**
         *
         * mystep - is the name of the component's .html template,
         * <Vendor>_<Module>  - is the name of the your module directory.
         *
         */
        return Component.extend({
            defaults: {
                template: 'Vendor_CustomCheckout/mystep'
            },
    
            //add here your logic to display step,
            isVisible: ko.observable(true),
    
            /**
             *
             * @returns {*}
             */
            initialize: function () {
                this._super();
                // register your step
                stepNavigator.registerStep(
                    //step code will be used as step content id in the component template
                    'custom_step',
                    //step alias
                    null,
                    //step title value
                    'Direccion de Facturacion',
                    //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 () {
                this.isVisible(false);
                this.isVisible = false;
            },
    
            /**
             * @returns void
             */
            navigateToNextStep: function () {
                // trigger form validation
                this.source.set('params.invalid', false);
                this.source.trigger('customStepForm.data.validate');
                console.dir(this.isVisible);
                // verify that form data is valid
                if (!this.source.get('params.invalid')) {
                    // data is retrieved from data provider by value of the customScope property
                    var formData = this.source.get('customStepForm');
                    // do something with form data
                    console.dir(formData);
                }
                stepNavigator.next();
            }
        });
    }
    );
    

    这是我的 Vendor_CustomCheckout / view / frontend / layout / checkout_index_index.xml

    <item name="custom-step" xsi:type="array">
        <item name="component" xsi:type="string">Vendor_CustomCheckout/js/view/step-view</item>
        <item name="provider" xsi:type="string">checkoutProvider</item>
        <item name="config" xsi:type="array">
            <item name="template" xsi:type="string">Vendor_CustomCheckout/mystep</item>
        </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">1.5</item>
        <item name="children" xsi:type="array">
        <!--add here child component declaration for your step-->
            <item name="custom-step-form-fieldset" xsi:type="array">
            <!-- uiComponent is used as a wrapper for form fields (its template will render all children as a list) -->
                <item name="component" xsi:type="string">uiComponent</item>
                <!-- the following display area is used in template (see below) -->
                <item name="displayArea" xsi:type="string">custom-step-form-fields</item>
                <item name="children" xsi:type="array">
                    <item name="name" xsi:type="array">
                        <item name="component" xsi:type="string">Magento_Ui/js/form/element/abstract</item>
                        <item name="config" xsi:type="array">
                            <!-- customScope is used to group elements within a single form (e.g. they can be validated separately) -->
                            <item name="customScope" xsi:type="string">customStepForm</item>
                            <item name="template" xsi:type="string">ui/form/field</item>
                            <item name="elementTmpl" xsi:type="string">ui/form/element/input</item>
                        </item>
                        <item name="provider" xsi:type="string">checkoutProvider</item>
                        <item name="dataScope" xsi:type="string">customStepForm.name</item>
                        <item name="label" xsi:type="string">Nombre</item>
                        <item name="sortOrder" xsi:type="string">1</item>
                        <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="string">true</item>
                        </item>
                    </item>                
            </item>
        </item>
    </item>
    

    谢谢,

    一个。马茨。

2 个答案:

答案 0 :(得分:1)

起初我遇到了同样的问题。原因在于以下几点:

Traceback (most recent call last):
  File "auth.py", line 8, in <module>
    response = requests.patch('https://example/answers/331', auth=HTTPBasicAuth('username', 'password'),json={"solution": "12345"})
  File "C:\Python27\lib\site-packages\requests-2.12.0-py2.7.egg\requests\api.py", line 138, in patch
    return request('patch', url,  data=data, **kwargs)
  File "C:\Python27\lib\site-packages\requests-2.12.0-py2.7.egg\requests\api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python27\lib\site-packages\requests-2.12.0-py2.7.egg\requests\sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python27\lib\site-packages\requests-2.12.0-py2.7.egg\requests\sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python27\lib\site-packages\requests-2.12.0-py2.7.egg\requests\adapters.py", line 473, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))

如果将其设置为true,它将立即显示您的步骤,因此要么为true建立条件,要么将其设置为false。这样,只有在完成装运步骤后才能看到您的步骤。

答案 1 :(得分:1)

这是适用于我们的解决方案

define(
    [
        'ko',
        'uiComponent',
        'underscore',
        'Magento_Checkout/js/model/step-navigator'
    ],
    function (
        ko,
        Component,
        _,
        stepNavigator
    ) {
        'use strict';
        /**
        *
        * newcheckout - is the name of the component's .html template, 
        * MD_Newcheckoutstep  - is the name of the your module directory.
        * 
        */
        return Component.extend({
            defaults: {
                template: 'MD_Newcheckoutstep/newcheckout'
            },

            //add here your logic to display step,
            // I have given false here so that is will no merge with other step
            // if you make it true sometime happens that is merge with shipping step.
            visible: ko.observable(quote.isVirtual())

            /**
            *
            * @returns {*}
            */
            initialize: function () {
                this._super();
                // register your step
                stepNavigator.registerStep(
                    //step code will be used as step content id in the component template
                    'newcheckoutstep',
                    //step alias
                    null,
                    //step title value
                    'New Checkout Step',
                    //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 () {
               self.visible(true);
            },

            /**
            * @returns void
            */
            navigateToNextStep: function () {
                stepNavigator.next();
            }
        });
    }
);

以下是有关此解决方案的更多详细信息on my own blog