条款和条件的验证(自定义模块)


9

我制作了一个自定义模块,使我可以将T&C复选框移动到摘要div中的付款方式和下订单按钮下方,如下所示:

结帐视图

现在,每当我选择一种付款方式时,请选中“条款和条件”中的复选框并按“下订单”,将弹出以下内容:

错误信息

我已经尝试了以下方法,但不幸的是没有运气:

Magento 2-在结帐中移动条款和条件的位置

提交订单后的Ajax有效负载:

在此处输入图片说明

在不使用模块的情况下使用结帐时,有效负载的内容(付款信息)具有:

extension_attributes: {agreement_ids: ["1"]}
agreement_ids: ["1"]

当使用我的自定义模块时,它如下:

extension_attributes: {agreement_ids: [""]}
agreement_ids: [""]

因此它是空的...这可能是问题所在。我该如何解决?

module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Checkout" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Checkout"/>
        </sequence>
    </module>
</config>

checkout_index_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <css src="Vendor_Checkout::css/checkout.css"/>
    </head>
    <body>


        <!-- Disabling the T&C checkbox under payment methods -->
        <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">
                                            <item name="billing-step" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <item name="payment" xsi:type="array">
                                                        <item name="children" xsi:type="array">
                                                            <item name="payments-list" xsi:type="array">
                                                                <item name="children" xsi:type="array">
                                                                    <item name="before-place-order" xsi:type="array">
                                                                        <item name="componentDisabled" xsi:type="boolean">true</item>
                                                                    </item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </argument>
                </arguments>
        </referenceBlock>

        <!-- Moving terms and conditions after payment methods -->
        <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">
                                        <item name="billing-step" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="payment" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <item name="afterMethods" xsi:type="array">
                                                            <item name="children" xsi:type="array">
                                                                <item name="agreements" xsi:type="array">
                                                                    <item name="component" xsi:type="string">Magento_CheckoutAgreements/js/view/checkout-agreements</item>
                                                                    <item name="sortOrder" xsi:type="string">100</item>
                                                                    <item name="displayArea" xsi:type="string">before-place-order</item>
                                                                    <item name="dataScope" xsi:type="string">checkoutAgreements</item>
                                                                    <item name="provider" xsi:type="string">checkoutProvider</item>
                                                                </item>
                                                                <item name="agreements-validator" xsi:type="array">
                                                                    <item name="component" xsi:type="string">Magento_CheckoutAgreements/js/view/agreement-validation</item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>

        <!-- Moving place order button to sidebar -->
        <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="sidebar" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="summary" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="children" xsi:type="array">
                                                    <item name="component" xsi:type="string">Vendor_Checkout/js/view/payment</item>
                                                    <item name="config" xsi:type="array">
                                                        <item name="template" xsi:type="string">Vendor_Checkout/button</item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>



    </body>
</page>

web / template / button.html

<!-- ko if: (isVisible()) -->
<div class="actions-toolbar-trigger" id="place-order-trigger-wrapper">
    <button type="button" class="button action primary" id="place-order-trigger" value="Place Order" >
        <span data-bind="i18n: 'Place Order'">Place Order</span>
    </button>
</div>
<!-- /ko -->

web / js / view / payment.js

define(
    [
        'jquery',
        'ko',
        'Magento_Checkout/js/view/summary',
        'Magento_Checkout/js/model/step-navigator'
    ],
    function(
        $,
        ko,
        Component,
        stepNavigator
    ) {
        'use strict';
        console.log('Running');
        return Component.extend({

            isVisible: function () {
                return stepNavigator.isProcessed('shipping');
            },
            initialize: function () {
                $(function() {
                    $('body').on("click", '#place-order-trigger', function () {

                        // Parse JSON here somehow?
                        $(".payment-method._active").find('.action.primary.checkout').trigger( 'click' );

                    });
                });
                var self = this;
                this._super();
            }

        });


    }
);

Answers:


3

因此,在缺席了一段时间之后,我发现了导致协议无法通过验证的原因。

该文件agreements-assigner.js位于中,

Module_CheckoutAgreements / view / frontend / web / js / model

在此文件中,有一个名为的变量agreementForm。该变量分配给条款和协议的复选框。最初是这样的:

agreementForm = $('.payment-method._active div[data-role=checkout-agreements] input');

但是,当然,我将复选框从其原始位置移开了,因此该复选框的路径不再存在。

例如,如果您更改变量,如下所示:

agreementForm = $('div[data-role=checkout-agreements] input');

.js文件能够找到您的Checkout Agreement复选框!现在可以成功下订单。

一个相当简单的修复。我试图使用自定义.js和东西修复它,并完全监督了这个文件...;)

注意:不要直接编辑核心文件。而是将其复制到模块或主题,然后根据需要更改文件。


@Bran,你能告诉我版本吗?
拉维·杜达拉

0

在您的自定义模块中添加以下行

app / code / Magento / CheckoutAgreements / view / frontend / web / js / view / checkout-agreements.js第48至77行

 /**
         * build a unique id for the term checkbox
         *
         * @param {Object} context - the ko context
         * @param {Number} agreementId
         */
        getCheckboxId: function (context, agreementId) {
            var paymentMethodName = '',
                paymentMethodRenderer = context.$parents[1];

            // corresponding payment method fetched from parent context
            if (paymentMethodRenderer) {
                // item looks like this: {title: "Check / Money order", method: "checkmo"}
                paymentMethodName = paymentMethodRenderer.item ?
                  paymentMethodRenderer.item.method : '';
            }

            return 'agreement_' + paymentMethodName + '_' + agreementId;
        },

        /**
         * Init modal window for rendered element
         *
         * @param {Object} element
         */
        initModal: function (element) {
            agreementsModal.createModal(element);
        }
    });
});

更改文件Magento / CheckoutAgreements / view / frontend / web / template / checkout / checkout-agreements.html


'id': 'agreement_' + agreementId,
在第14行中替换

 'id': $parent.getCheckboxId($parentContext, agreementId),

更换

<label data-bind="attr: {'for': 'agreement_' + agreementId}">

<label data-bind="attr: {'for': $parent.getCheckboxId($parentContext, agreementId)}">

第18行

参考https : //github.com/magento/magento2/commit/14b9b9813c9a16c0c45505885503cfb71bc4eb8d


我已经使用了最新版本的Magento 2(2.2.3)。在此版本中,所有代码均已如您所述。如果我在答案中分享代码,对您有用吗?
神鹰

嗨@lalit mohan,我使用的是默认的magento 2.2.4,一旦我点击了下单按钮,就会遇到相同的错误。能否请您详细回答答案
jafar pinjar
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.