Magento 2.3.3 set product as New date issue

All we need is an easy explanation of the problem, so here it is.

I am having an issue with Magento 2.3.3 and set up as new to Date.

Although I set the date in a product in admin, if I go back to the product and press save the date for set up product as new to is gone.
This is really strange.
So I thought to create a simple module to keep that date just before saving the product. Also I inserted a function to give the product if the date is not set 90days + the date is when you save. But it isn’t working and I don’t see any errors in logs

My code is:

namespace MyModule\DatesFix\Plugin\Backend\Magento\Catalog\Model;

/**
 * Class Product
 * @package Mymodule\DatesFix\Plugin\Backend\Magento\Catalog\Model
 */
class Product
{

    /**
     * Before save
     *
     * @param \Magento\Catalog\Model\Product $subject
     *
     *
     * @return array
     */
    public function beforeBeforeSave(
        \Magento\Catalog\Model\Product $subject
    ) {
        $newDateTo = date('Y-m-d h:m:s', strtotime("+90 day"));
        $from = $subject->getNewsFromDate();
        $to = $subject->getNewsToDate();
        if ($from && $to) {
            $subject->setNewsToDate($newDateTo);
        }

        return [];
    }
}

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

you need to create a custom extension with the following :

MyModule/DatesFix/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'MyModule_DatesFix',
    __DIR__
);

MyModule/DatesFix/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MyModule_DatesFix" setup_version="1.0.0"></module>
</config>

MyModule/DatesFix/etc/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">
    <type name="Magento\Catalog\Model\Product">
        <plugin disabled="false" name="new_to_datefix" sortOrder="10" type="MyModule\DatesFix\Plugin\NewToDate"/>
    </type>
</config>

In the above di.xml file we have declared the name="Magento\Catalog\Model\Product" , this refers to the class whose method we are going to override .

so you can find all the related methods that you can use by going to vendor/module-catalog/Model/Product.php file .

MyModule/DatesFix/Plugin/NewToDate.php

<?php 

namespace MyModule\DatesFix\Plugin;

class NewToDate
{
    public function beforeSave($subject){
        $new_to_date = date('m/d/Y',strtotime("+90 days"));

        if (!$subject->getNewsToDate()) {
            $subject->setNewsToDate($new_to_date); 
        }
    }
}

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply