diff --git a/app/code/Magento/Catalog/Model/Attribute/Backend/Startdate.php b/app/code/Magento/Catalog/Model/Attribute/Backend/Startdate.php index 7f7fa8ba62aed..e81d6b0d59404 100644 --- a/app/code/Magento/Catalog/Model/Attribute/Backend/Startdate.php +++ b/app/code/Magento/Catalog/Model/Attribute/Backend/Startdate.php @@ -47,9 +47,6 @@ protected function _getValueForSave($object) if ($startDate === false) { return false; } - if ($startDate == '' && $object->getSpecialPrice()) { - $startDate = $this->_localeDate->date(); - } return $startDate; } diff --git a/app/code/Magento/Catalog/Observer/SetSpecialPriceStartDateObserver.php b/app/code/Magento/Catalog/Observer/SetSpecialPriceStartDateObserver.php new file mode 100644 index 0000000000000..640509fb12d79 --- /dev/null +++ b/app/code/Magento/Catalog/Observer/SetSpecialPriceStartDateObserver.php @@ -0,0 +1,42 @@ +localeDate = $localeDate; + } + + /** + * Setting Special Price start date + * + * @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()) { + $product->setData('special_from_date', $this->localeDate->date()); + } + + return $this; + } +} diff --git a/app/code/Magento/Catalog/etc/events.xml b/app/code/Magento/Catalog/etc/events.xml index a495a47fe9da0..cfd5e35e4ab3e 100644 --- a/app/code/Magento/Catalog/etc/events.xml +++ b/app/code/Magento/Catalog/etc/events.xml @@ -51,4 +51,7 @@ + + + diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php index aa3e33996636b..8441d4155bfa8 100644 --- a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php @@ -28,6 +28,7 @@ class ProductRepositoryInterfaceTest extends WebapiAbstract const RESOURCE_PATH = '/V1/products'; const KEY_TIER_PRICES = 'tier_prices'; + const KEY_SPECIAL_PRICE = 'special_price'; const KEY_CATEGORY_LINKS = 'category_links'; /** @@ -1122,4 +1123,21 @@ private function getMediaGalleryData($filename1, $encodedImage, $filename2) ], ]; } + + public function testSpecialPrice() + { + $productData = $this->getSimpleProductData(); + $productData['custom_attributes'] = [ + ['attribute_code' => self::KEY_SPECIAL_PRICE, 'value' => '1'] + ]; + $this->saveProduct($productData); + $response = $this->getProduct($productData[ProductInterface::SKU]); + $customAttributes = $response['custom_attributes']; + $this->assertNotEmpty($customAttributes); + $missingAttributes = ['news_from_date', 'custom_design_from']; + $expectedAttribute = ['special_price', 'special_from_date']; + $attributeCodes = array_column($customAttributes, 'attribute_code'); + $this->assertEquals(0, count(array_intersect($attributeCodes, $missingAttributes))); + $this->assertEquals(2, count(array_intersect($attributeCodes, $expectedAttribute))); + } }