Question

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

Was it helpful?

Solution

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 ...

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top