0

I have changed the checkout process into 4 steps

  1. Adress
  2. Shipping
  3. Payment methods and agreement
  4. Summary and place order button

everything is working fine accept the terms and condition. I have put the validation that user has to check the box for agreement other wise he wont be able to move to the last step i.e (summary and place order button). the validation is working fine, but when user check the agreement box then he able to move himself on the last step. and when he click on place order function I get the error of

The order wasn't placed.First, agree to the terms and conditions, then try placing your order again.

do I need to send some ajax request for agreement or attach something with place order button so i don't get this error.

this is what my placeorder function looks like

placeorder: function(){
            $('.payment-method._active .primary button.checkout').trigger('click');
        },

if I try with the setting from admin panel to agreement automatic but it wont work on manually. how can i do it manually?

what I found till is this file is responsible for error

vendor/magento/module-checkout/Model/PaymentInformationManagement.php

how i can override this file?

1
  • How you added term & conditions checkbox on the checkout? Commented Mar 26 at 6:44

1 Answer 1

0

i have debugged it. Apparetnly it was the fault with magento itself and the valdation was error was coming from

vendor/magento/module-checkout-agreements/Model/Checkout/Plugin/Validation.php

and you have to create a prefence in wether by creating your own custom module or already built your module in

app/code directory

so your path for file will become like this

/app/code/TwoFish/SplitShippingMethod/Model/Checkout/Plugin/Validation.php

and then you need to create a di.xml in your same module

/app/code/TwoFish/SplitShippingMethod/etc/di.xml

and the content of both files will be for Validation.php

    <?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace TwoFish\SplitShippingMethod\Model\Checkout\Plugin;

use Magento\CheckoutAgreements\Model\AgreementsProvider;
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Store\Model\ScopeInterface;

/**
 * Class Validation validates the agreement based on the payment method
 */
class Validation
{
      /**
       * @var \Magento\Framework\App\Config\ScopeConfigInterface
       */
      private $scopeConfiguration;

      /**
       * @var \Magento\Checkout\Api\AgreementsValidatorInterface
       */
      private $agreementsValidator;

      /**
       * @var \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface
       */
      private $checkoutAgreementsList;

      /**
       * @var \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter
       */
      private $activeStoreAgreementsFilter;

      /**
       * Quote repository.
       *
       * @var \Magento\Quote\Api\CartRepositoryInterface
       */
      private $quoteRepository;

      /**
       * @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator
       * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
       * @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList
       * @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
       * @param CartRepositoryInterface $quoteRepository
       */
      public function __construct(
           \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
           \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
           \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
           \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter,
           CartRepositoryInterface $quoteRepository
      ) {
            $this->agreementsValidator = $agreementsValidator;
            $this->scopeConfiguration = $scopeConfiguration;
            $this->checkoutAgreementsList = $checkoutAgreementsList;
            $this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
            $this->quoteRepository = $quoteRepository;
      }

      /**
       * Validates agreements before save payment information and  order placing.
       *
       * @param \Magento\Checkout\Api\PaymentInformationManagementInterface $subject
       * @param int $cartId
       * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
       * @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
       * @throws \Magento\Framework\Exception\CouldNotSaveException
       * @return void
       * @SuppressWarnings(PHPMD.UnusedFormalParameter)
       */
      public function beforeSavePaymentInformationAndPlaceOrder(
           \Magento\Checkout\Api\PaymentInformationManagementInterface $subject,
                                                                       $cartId,
           \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
           \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
      ) {
            /*if ($this->isAgreementEnabled()) {
                $this->validateAgreements($paymentMethod);
            }*/
      }

      /**
       * Validate agreements base on the payment method
       *
       * @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
       * @throws \Magento\Framework\Exception\CouldNotSaveException
       * @return void
       */
      protected function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
      {
            $agreements = $paymentMethod->getExtensionAttributes() === null
                 ? []
                 : $paymentMethod->getExtensionAttributes()->getAgreementIds();

            if (!$this->agreementsValidator->isValid($agreements)) {
                  throw new \Magento\Framework\Exception\CouldNotSaveException(
                       __(
                            "The order wasn't placed. "
                            . "First, agree to the terms and conditions, then try placing your order again."
                       )
                  );
            }
      }

      /**
       * Verify if agreement validation needed.
       *
       * @return bool
       */
      private function isAgreementEnabled()
      {
            $isAgreementsEnabled = $this->scopeConfiguration->isSetFlag(
                 AgreementsProvider::PATH_ENABLED,
                 ScopeInterface::SCOPE_STORE
            );
            $agreementsList = $isAgreementsEnabled
                 ? $this->checkoutAgreementsList->getList($this->activeStoreAgreementsFilter->buildSearchCriteria())
                 : [];
            return (bool)($isAgreementsEnabled && count($agreementsList) > 0);
      }
}

and the content of di.xml file will be like this

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\CheckoutAgreements\Model\Checkout\Plugin\Validation" type="TwoFish\SplitShippingMethod\Model\Checkout\Plugin\Validation" /> </config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.