cancel
Showing results for 
Search instead for 
Did you mean: 

Disable past dates in magento default calender

SOLVED
Highlighted

Disable past dates in magento default calender

How to override vendor/magento/framework/View/Element/Html/Date.php? 

Please help me out with this. 

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted

Re: Disable past dates in magento default calender

Okay, you can override by following approach :

  1. Create Date Class in your new module :
    <?php
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    namespace Vendor\Module\Model;
    //namespace Magento\Framework\View\Element\Html;
    
    /**
     * Date element block
     */
    class Date extends \Magento\Framework\View\Element\Html\Date
    {
        /**
         * Render block HTML
         *
         * @return string
         * @SuppressWarnings(PHPMD.NPathComplexity)
         */
        protected function _toHtml()
        {
            $html = '<input type="text" name="' . $this->getName() . '" id="' . $this->getId() . '" ';
            $html .= 'value="' . $this->escapeHtml($this->getValue()) . '" ';
            $html .= 'class="' . $this->getClass() . '" ' . $this->getExtraParams() . '/> ';
            $calendarYearsRange = $this->getYearsRange();
            $changeMonth = $this->getChangeMonth();
            $changeYear = $this->getChangeYear();
            $maxDate = $this->getMaxDate();
            $showOn = $this->getShowOn();
            $firstDay = $this->getFirstDay();
    
            $html .= '<span style="display:none;">123</span><script type="text/javascript">
                require(["jquery", "mage/calendar"], function($){
                        $("#' .
                $this->getId() .
                '").calendar({
                            showsTime: ' .
                ($this->getTimeFormat() ? 'true' : 'false') .
                ',
                            ' .
                ($this->getTimeFormat() ? 'timeFormat: "' .
                $this->getTimeFormat() .
                '",' : '') .
                '
                            dateFormat: "' .
                $this->getDateFormat() .
                '",
                            buttonImage: "' .
                $this->getImage() .
                '",
                            ' .
                ($calendarYearsRange ? 'yearRange: "' .
                $calendarYearsRange .
                '",' : '') .
                '
                            buttonText: "' .
                (string)new \Magento\Framework\Phrase(
                    'Select Date'
                ) .
                '"' . ($maxDate ? ', maxDate: "' . $maxDate . '"' : '') .
                ($changeMonth === null ? '' : ', changeMonth: ' . $changeMonth) .
                ($changeYear === null ? '' : ', changeYear: ' . $changeYear) .
                ($showOn ? ', showOn: "' . $showOn . '"' : '') .
                ($firstDay ? ', firstDay: ' . $firstDay : '') .
                '})
                });
                </script>';
    
            return $html;
        }
    
        /**
         * Convert special characters to HTML entities
         *
         * @return string
         */
        public function getEscapedValue()
        {
            if ($this->getFormat() && $this->getValue()) {
                return strftime($this->getFormat(), strtotime($this->getValue()));
            }
            return $this->escapeHtml($this->getValue());
        }
    
        /**
         * Produce and return block's html output
         *
         * {@inheritdoc}
         *
         * @return string
         */
        public function getHtml()
        {
            return $this->toHtml();
        }
    }
    ?>
  2. Create di.xml :
    <?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\Framework\View\Element\Html\Date" type="Vendor\Module\Model\Date"/>
    </config>
    Copy code from your core class and paste in above class file.

Hope it helps !

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !

View solution in original post

Highlighted

Re: Disable past dates in magento default calender

Yes, you should create a new module, please find a reference below :

https://devdocs.magento.com/videos/fundamentals/create-a-new-module/

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !

View solution in original post

8 REPLIES 8
Highlighted

Re: Disable past dates in magento default calender

Hello @waqassadafa462 

 

You can take this reference : 

https://magento.stackexchange.com/questions/99845/disable-previous-days-in-default-magento2-calendar

 

But this will overrride all the calendar in Magento scope, suppose you want to change the catalog sale from past sale or make a product available from past time, you will not longer able to change it after adding once.

better you can change as per required page.

 

Hope it helps !

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !
Highlighted

Re: Disable past dates in magento default calender

Hallo, there is a calendar function in date.php. i have written minDate: new Date() over there then i am able to disable past dates. But when magento will update then edited past will be no longer. So, i have to override that date.php file. I have a main javascript file when i write that function there but it doesn't work so i have no other option. Please help me out with this

Highlighted

Re: Disable past dates in magento default calender

Okay, you can override by following approach :

  1. Create Date Class in your new module :
    <?php
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    namespace Vendor\Module\Model;
    //namespace Magento\Framework\View\Element\Html;
    
    /**
     * Date element block
     */
    class Date extends \Magento\Framework\View\Element\Html\Date
    {
        /**
         * Render block HTML
         *
         * @return string
         * @SuppressWarnings(PHPMD.NPathComplexity)
         */
        protected function _toHtml()
        {
            $html = '<input type="text" name="' . $this->getName() . '" id="' . $this->getId() . '" ';
            $html .= 'value="' . $this->escapeHtml($this->getValue()) . '" ';
            $html .= 'class="' . $this->getClass() . '" ' . $this->getExtraParams() . '/> ';
            $calendarYearsRange = $this->getYearsRange();
            $changeMonth = $this->getChangeMonth();
            $changeYear = $this->getChangeYear();
            $maxDate = $this->getMaxDate();
            $showOn = $this->getShowOn();
            $firstDay = $this->getFirstDay();
    
            $html .= '<span style="display:none;">123</span><script type="text/javascript">
                require(["jquery", "mage/calendar"], function($){
                        $("#' .
                $this->getId() .
                '").calendar({
                            showsTime: ' .
                ($this->getTimeFormat() ? 'true' : 'false') .
                ',
                            ' .
                ($this->getTimeFormat() ? 'timeFormat: "' .
                $this->getTimeFormat() .
                '",' : '') .
                '
                            dateFormat: "' .
                $this->getDateFormat() .
                '",
                            buttonImage: "' .
                $this->getImage() .
                '",
                            ' .
                ($calendarYearsRange ? 'yearRange: "' .
                $calendarYearsRange .
                '",' : '') .
                '
                            buttonText: "' .
                (string)new \Magento\Framework\Phrase(
                    'Select Date'
                ) .
                '"' . ($maxDate ? ', maxDate: "' . $maxDate . '"' : '') .
                ($changeMonth === null ? '' : ', changeMonth: ' . $changeMonth) .
                ($changeYear === null ? '' : ', changeYear: ' . $changeYear) .
                ($showOn ? ', showOn: "' . $showOn . '"' : '') .
                ($firstDay ? ', firstDay: ' . $firstDay : '') .
                '})
                });
                </script>';
    
            return $html;
        }
    
        /**
         * Convert special characters to HTML entities
         *
         * @return string
         */
        public function getEscapedValue()
        {
            if ($this->getFormat() && $this->getValue()) {
                return strftime($this->getFormat(), strtotime($this->getValue()));
            }
            return $this->escapeHtml($this->getValue());
        }
    
        /**
         * Produce and return block's html output
         *
         * {@inheritdoc}
         *
         * @return string
         */
        public function getHtml()
        {
            return $this->toHtml();
        }
    }
    ?>
  2. Create di.xml :
    <?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\Framework\View\Element\Html\Date" type="Vendor\Module\Model\Date"/>
    </config>
    Copy code from your core class and paste in above class file.

Hope it helps !

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !

View solution in original post

Re: Disable past dates in magento default calender

Run below commands after that :

php bin/magento setup:di:compile
php bin/magento c:f
Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !
Highlighted

Re: Disable past dates in magento default calender

Should i create a module folder and then new folder modal and create date.php there ? I have a folder in app with the name of cars

Highlighted

Re: Disable past dates in magento default calender

Yes, you should create a new module, please find a reference below :

https://devdocs.magento.com/videos/fundamentals/create-a-new-module/

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !

View solution in original post

Highlighted

Re: Disable past dates in magento default calender

Smiley Happy

Highlighted

Re: Disable past dates in magento default calender

fehler_calendar.PNG