1

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 [];
    }
}
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); 
        }
    }
}

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.