1

We use delivery date: https://github.com/sohelrana09/magento2-module-delivery-date/tree/master/SR/DeliveryDate

Currently this field is required. Any solution how to uncheck to set this filed not required?

enter image description here

2 Answers 2

1

Add this code from remove required field

/code/SR/DeliveryDate/Observer/SalesModelServiceQuoteSubmitBefore.php

<?php
namespace SR\DeliveryDate\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\QuoteRepository;
use SR\DeliveryDate\Model\Validator;

class SalesModelServiceQuoteSubmitBefore implements ObserverInterface
{
    /**
     * @var QuoteRepository
     */
    private $quoteRepository;

    /**
     * @var Validator
     */
    private $validator;

    /**
     * SalesModelServiceQuoteSubmitBefore constructor.
     *
     * @param QuoteRepository $quoteRepository
     * @param Validator $validator
     */
    public function __construct(
        QuoteRepository $quoteRepository,
        Validator $validator
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->validator = $validator;
    }

    /**
     * @param EventObserver $observer
     * @return $this
     * @throws \Exception
     */
    public function execute(EventObserver $observer)
    {
        $order = $observer->getOrder();
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->quoteRepository->get($order->getQuoteId());
        if (empty($order))
        {
        $order->setDeliveryDate($quote->getDeliveryDate());
        }

        $order->setDeliveryDate($quote->getDeliveryDate());
        $order->setDeliveryComment($quote->getDeliveryComment());

        return $this;
    }
}

Thanks ...

1

Please see this commit in your shared extension https://github.com/sohelrana09/magento2-module-delivery-date/commit/50eaa5e798b2cfd08c7e60d5117b577d9b7a93df it seems this extension control whether the field is required or not based on the store configuration.

const XPATH_REQUIRED_DELIVERY_DATE = 'sr_deliverydate/general/required_delivery_date';

We can find the configuration below.

Admin -> Store -> Configuration -> Order Delivery Date Settings -> Extension Settings -> Required Delivery Date

Updates: As per the comments you put, it seems observer sales_model_service_quote_submit_before didn't check store configuration before throw exception so for the quick fix I have added that logic

<?php
namespace SR\DeliveryDate\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\QuoteRepository;
use SR\DeliveryDate\Model\Validator;
use Magento\Framework\App\Config\ScopeConfigInterface;

class SalesModelServiceQuoteSubmitBefore implements ObserverInterface
{
    /**
     * @var QuoteRepository
     */
    private $quoteRepository;

    /**
     * @var Validator
     */
    private $validator;

    /**
     * @var Validator
     */
    private $scopeConfig;

    /**
     * SalesModelServiceQuoteSubmitBefore constructor.
     *
     * @param QuoteRepository $quoteRepository
     * @param Validator $validator
     */
    public function __construct(
        QuoteRepository $quoteRepository,
        Validator $validator,
        ScopeConfigInterface $scopeConfig
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->validator = $validator;
        $this->scopeConfig = $scopeConfig;
    }

    /**
     * @param EventObserver $observer
     * @return $this
     * @throws \Exception
     */
    public function execute(EventObserver $observer)
    {
        $order = $observer->getOrder();
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->quoteRepository->get($order->getQuoteId());

        // get the store configuration about required settings
        $is_required = $this->scopeConfig->getValue('sr_deliverydate/general/required_delivery_date', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

        //if it is not required and the value is empty/null then return the value without any changes.print $quote->getDeliveryDate() and then update if condition as per your need
        if(!$is_required && $quote->getDeliveryDate()==''){
            return $this;
        }

        // if it is required or not required, but customer enter the value so validate the value and return the exception
        if (!$this->validator->validate($quote->getDeliveryDate())) {
            throw new \Exception(__('Invalid Delevery Date'));
        }

        $order->setDeliveryDate($quote->getDeliveryDate());
        $order->setDeliveryComment($quote->getDeliveryComment());

        return $this;
    }
}

I hope it gives some idea.

Note: I didn't install this module, I just reviewed the code you shared and update the answer.

7
  • Yes, I set this option in backend. DeliveryDate is required: no. After save in last step checkout during select payment method impossible finish order. In log file I see: incorrect DeliveryDate
    – Sylvester
    Apr 26, 2020 at 17:50
  • have you flushed the config cache? test it in a private window. Apr 26, 2020 at 17:53
  • Yes, cache cleaned. For information: this option in module is set directly after install extension. DeliveryDate required: no
    – Sylvester
    Apr 26, 2020 at 17:54
  • please post the log details with a full trace? are you using php-fpm? if yes we need to restart fpm as well. Similarly check varnish, redis. Apr 26, 2020 at 17:57
  • [2020-04-26 17:56:03] main.CRITICAL: Invalid Delevery Date {"exception":"[object] (Exception(code: 0): Invalid Delevery Date at /home/usr/domains/domain.com/public_html/app/code/SR/DeliveryDate/Observer/SalesModelServiceQuoteSubmitBefore.php:46)"} []
    – Sylvester
    Apr 26, 2020 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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