Magento – Magento 2: Terms and Conditions Validation issue

magento2terms and conditionsvalidation

I am working with Magento 2.1.0. I have enabled 'Terms and conditions' on site and added 2 conditions one for 'Terms and conditions' and other for 'Replacement Policy'. It displayed both in checkout page correctly, but validation is done only for the first check-box. Not considering the second one. If first check-box is checked. We will be able to place the order.

I have found out that it is the issue with the Magento 2. How can I fix this issue? Please help.

Best Answer

We have face the same issue(terms and conditions not validating) in our system, and change the below mentioned code. Now working properly.

File Path:

vendor/magento/module-checkout-agreements/view/frontend/web/js/model/agreement-validator.js

Change the below code:

/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
/*jshint browser:true jquery:true*/
/*global alert*/
define(
    [
        'jquery',
        'mage/validation'
    ],
    function ($) {
        'use strict';
        var checkoutConfig = window.checkoutConfig,
            agreementsConfig = checkoutConfig ? checkoutConfig.checkoutAgreements : {};

        var agreementsInputPath = '.payment-method._active div.checkout-agreements input';

        return {
            /**
             * Validate checkout agreements
             *
             * @returns {boolean}
             */
            validate: function() {
                var noError = true;
                if (!agreementsConfig.isEnabled || $(agreementsInputPath).length == 0) {
                    return noError;
                }

                $('.payment-method:not(._active) div.checkout-agreements input')
                    .prop('checked', false)
                    .removeClass('mage-error')
                    .siblings('.mage-error[generated="true"]').remove();

                $(agreementsInputPath).each(function() {
                    var name = $(this).attr('name');

                    var result = $('#co-payment-form').validate({
                        errorClass: 'mage-error',
                        errorElement: 'div',
                        meta: 'validate',
                        errorPlacement: function (error, element) {
                            var errorPlacement = element;
                            if (element.is(':checkbox') || element.is(':radio')) {
                                errorPlacement = element.siblings('label').last();
                            }
                            errorPlacement.after(error);
                        }
                    }).element(agreementsInputPath + '[name="' + name + '"]');

                    if (!result) {
                        noError = false;
                    }
                });

                return noError;
            }
        }
    }
);

After changed the code you will following command:

php bin/magento setup:static-content:deploy
php bin/magento cache:clean
php bin/magento cache:flush
Related Topic