Skip to content

Commit a24a47f

Browse files
committed
MAGETWO-52577: [GitHub] Set Product as New from Date and Design Active From is set when setting Special Price magento#4387
1 parent bbb180f commit a24a47f

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

app/code/Magento/Catalog/Model/Attribute/Backend/Startdate.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ protected function _getValueForSave($object)
4747
if ($startDate === false) {
4848
return false;
4949
}
50-
if ($startDate == '' && $object->getSpecialPrice()) {
51-
$startDate = $this->_localeDate->date();
52-
}
5350

5451
return $startDate;
5552
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Observer;
7+
8+
use Magento\Framework\Event\ObserverInterface;
9+
10+
class SetSpecialPriceStartDateObserver implements ObserverInterface
11+
{
12+
/**
13+
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
14+
*/
15+
protected $localeDate;
16+
17+
/**
18+
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
19+
* @codeCoverageIgnore
20+
*/
21+
public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate)
22+
{
23+
$this->localeDate = $localeDate;
24+
}
25+
26+
/**
27+
* Setting Special Price start date
28+
*
29+
* @param \Magento\Framework\Event\Observer $observer
30+
* @return $this
31+
*/
32+
public function execute(\Magento\Framework\Event\Observer $observer)
33+
{
34+
/** @var $product \Magento\Catalog\Model\Product */
35+
$product = $observer->getEvent()->getProduct();
36+
if ($product->getSpecialPrice() && !$product->getSpecialFromDate()) {
37+
$product->setData('special_from_date', $this->localeDate->date());
38+
}
39+
40+
return $this;
41+
}
42+
}

app/code/Magento/Catalog/etc/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,7 @@
5151
<event name="magento_catalog_api_data_categorytreeinterface_load_after">
5252
<observer name="legacy_categorytree_load_after" instance="Magento\Framework\EntityManager\Observer\AfterEntityLoad" />
5353
</event>
54+
<event name="catalog_product_save_before">
55+
<observer name="set_special_price_start_date" instance="Magento\Catalog\Observer\SetSpecialPriceStartDateObserver" />
56+
</event>
5457
</config>

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ProductRepositoryInterfaceTest extends WebapiAbstract
2828
const RESOURCE_PATH = '/V1/products';
2929

3030
const KEY_TIER_PRICES = 'tier_prices';
31+
const KEY_SPECIAL_PRICE = 'special_price';
3132
const KEY_CATEGORY_LINKS = 'category_links';
3233

3334
/**
@@ -1122,4 +1123,21 @@ private function getMediaGalleryData($filename1, $encodedImage, $filename2)
11221123
],
11231124
];
11241125
}
1126+
1127+
public function testSpecialPrice()
1128+
{
1129+
$productData = $this->getSimpleProductData();
1130+
$productData['custom_attributes'] = [
1131+
['attribute_code' => self::KEY_SPECIAL_PRICE, 'value' => '1']
1132+
];
1133+
$this->saveProduct($productData);
1134+
$response = $this->getProduct($productData[ProductInterface::SKU]);
1135+
$customAttributes = $response['custom_attributes'];
1136+
$this->assertNotEmpty($customAttributes);
1137+
$missingAttributes = ['news_from_date', 'custom_design_from'];
1138+
$expectedAttribute = ['special_price', 'special_from_date'];
1139+
$attributeCodes = array_column($customAttributes, 'attribute_code');
1140+
$this->assertEquals(0, count(array_intersect($attributeCodes, $missingAttributes)));
1141+
$this->assertEquals(2, count(array_intersect($attributeCodes, $expectedAttribute)));
1142+
}
11251143
}

0 commit comments

Comments
 (0)