Skip to content

Commit eb84dae

Browse files
committed
MAGETWO-61315: [Backport] - [GitHub] Set Product as New from Date and Design Active From is set when setting Special Price magento#4387 - for 2.1
1 parent 6065b1c commit eb84dae

File tree

7 files changed

+79
-9
lines changed

7 files changed

+79
-9
lines changed

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ protected function _getValueForSave($object)
4444
{
4545
$attributeName = $this->getAttribute()->getName();
4646
$startDate = $object->getData($attributeName);
47-
if ($startDate === false) {
48-
return false;
49-
}
50-
if ($startDate == '' && $object->getSpecialPrice()) {
51-
$startDate = $this->_localeDate->date();
52-
}
5347

5448
return $startDate;
5549
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
/**
11+
* Set value for Special Price start date
12+
*/
13+
class SetSpecialPriceStartDate implements ObserverInterface
14+
{
15+
/**
16+
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
17+
*/
18+
private $localeDate;
19+
20+
/**
21+
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
22+
* @codeCoverageIgnore
23+
*/
24+
public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate)
25+
{
26+
$this->localeDate = $localeDate;
27+
}
28+
29+
/**
30+
* Set the current date to Special Price From attribute if it empty
31+
*
32+
* @param \Magento\Framework\Event\Observer $observer
33+
* @return $this
34+
*/
35+
public function execute(\Magento\Framework\Event\Observer $observer)
36+
{
37+
/** @var $product \Magento\Catalog\Model\Product */
38+
$product = $observer->getEvent()->getProduct();
39+
if ($product->getSpecialPrice() && !$product->getSpecialFromDate()) {
40+
$product->setData('special_from_date', $this->localeDate->date());
41+
}
42+
43+
return $this;
44+
}
45+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
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\SetSpecialPriceStartDate" />
56+
</event>
5457
<event name="admin_system_config_changed_section_catalog">
5558
<observer name="catalog_update_price_attribute" instance="Magento\Catalog\Observer\SwitchPriceAttributeScopeOnConfigChange" />
5659
</event>

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

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

2323
const KEY_TIER_PRICES = 'tier_prices';
24+
const KEY_SPECIAL_PRICE = 'special_price';
2425

2526
/**
2627
* @var array
@@ -828,4 +829,21 @@ private function getStockItemData()
828829
StockItemInterface::STOCK_STATUS_CHANGED_AUTO => 0,
829830
];
830831
}
832+
833+
public function testSpecialPrice()
834+
{
835+
$productData = $this->getSimpleProductData();
836+
$productData['custom_attributes'] = [
837+
['attribute_code' => self::KEY_SPECIAL_PRICE, 'value' => '1']
838+
];
839+
$this->saveProduct($productData);
840+
$response = $this->getProduct($productData[ProductInterface::SKU]);
841+
$customAttributes = $response['custom_attributes'];
842+
$this->assertNotEmpty($customAttributes);
843+
$missingAttributes = ['news_from_date', 'custom_design_from'];
844+
$expectedAttribute = ['special_price', 'special_from_date'];
845+
$attributeCodes = array_column($customAttributes, 'attribute_code');
846+
$this->assertEquals(0, count(array_intersect($attributeCodes, $missingAttributes)));
847+
$this->assertEquals(2, count(array_intersect($attributeCodes, $expectedAttribute)));
848+
}
831849
}

dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Product/ProductForm.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@
4545
<visibility>
4646
<input>select</input>
4747
</visibility>
48-
<news_from_date />
48+
<news_from_date>
49+
<selector>[name="product[news_from_date]"]</selector>
50+
<strategy>css selector</strategy>
51+
</news_from_date>
4952
<news_to_date>
5053
<selector>[name="product[news_to_date]"]</selector>
5154
<strategy>css selector</strategy>
@@ -198,6 +201,10 @@
198201
<selector>[data-index="type"]</selector>
199202
<strategy>css selector</strategy>
200203
</type>
204+
<custom_design_from>
205+
<selector>[name="product[custom_design_from]"]</selector>
206+
<strategy>css selector</strategy>
207+
</custom_design_from>
201208
</fields>
202209
</customer-options>
203210
<gift-options>

dev/tests/functional/tests/app/Magento/Catalog/Test/Fixture/CatalogProductSimple.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
<field name="country_of_manufacture" is_required="0" />
3030
<field name="created_at" is_required="1" />
3131
<field name="custom_design" is_required="0" />
32-
<field name="custom_design_from" is_required="0" />
33-
<field name="custom_design_to" is_required="0" />
32+
<field name="custom_design_from" is_required="0" group="schedule-design-update" source="Magento\Backend\Test\Fixture\Source\Date" />
33+
<field name="custom_design_to" is_required="0" group="schedule-design-update" source="Magento\Backend\Test\Fixture\Source\Date" />
3434
<field name="custom_layout_update" is_required="0" />
3535
<field name="description" is_required="0" group="content" />
3636
<field name="gallery" is_required="0" />

dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Product/CreateSimpleProductEntityTest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@
5555
<data name="product/data/short_description" xsi:type="string">Simple Product short_description %isolation%</data>
5656
<data name="product/data/description" xsi:type="string">Simple Product description %isolation%</data>
5757
<data name="product/data/weight" xsi:type="string">52</data>
58+
<data name="product/data/news_from_date" xsi:type="string"></data>
59+
<data name="product/data/custom_design_from" xsi:type="string"></data>
5860
<data name="product/data/quantity_and_stock_status/qty" xsi:type="string">659</data>
5961
<data name="product/data/custom_options/dataset" xsi:type="string">drop_down_with_one_option_fixed_price</data>
6062
<data name="product/data/checkout_data/dataset" xsi:type="string">simple_drop_down_with_one_option_fixed_price</data>
6163
<data name="product/data/price/dataset" xsi:type="string">MAGETWO-23029</data>
6264
<constraint name="Magento\Catalog\Test\Constraint\AssertProductSaveMessage" />
65+
<constraint name="Magento\Catalog\Test\Constraint\AssertProductForm" />
6366
<constraint name="Magento\Catalog\Test\Constraint\AssertProductInGrid" />
6467
<constraint name="Magento\Catalog\Test\Constraint\AssertProductInCategory" />
6568
<constraint name="Magento\Catalog\Test\Constraint\AssertProductPage" />

0 commit comments

Comments
 (0)