Magically special_from_date is set whenever product is updated
-
14-04-2021 - |
سؤال
I got a weird issue i can't see to find the reason behind it.
When i update product with a special_price
and the special_from_date
is NULL (empty) it updates the special_from_date
to today.. And i can't seem to find why..
Is this a common bug? (i don't think so) but i also can't seem to find the root of the problem. thanks in advance!
And for a short workaround how can i safely disable special_from_date or just don't let it update?
المحلول
It's not a bug, it's a feature :-)
The "magic" happens in the observer Magento\Catalog\Observer\SetSpecialPriceStartDate
which is executed in the event catalog_product_save_before
and does exactly what you are describing:
/**
* Set the current date to Special Price From attribute if it's empty.
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var $product \Magento\Catalog\Model\Product */
$product = $observer->getEvent()->getProduct();
if ($product->getSpecialPrice() && $product->getSpecialFromDate() === null) {
$product->setData('special_from_date', $this->localeDate->date()->setTime(0, 0));
}
return $this;
}
You can disable this observer if you don't want that to happen. Just add the following code into an events.xml file into your module:
<event name="catalog_product_save_before">
<observer name="set_special_price_start_date" disabled="true" />
</event>