Skip to content

Commit a2d59ac

Browse files
committed
MAGETWO-52577: [GitHub] Set Product as New from Date and Design Active From is set when setting Special Price #4387
2 parents 2bb7990 + 4147d9d commit a2d59ac

File tree

7 files changed

+83
-11
lines changed

7 files changed

+83
-11
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
@@ -54,4 +54,7 @@
5454
<event name="admin_system_config_changed_section_catalog">
5555
<observer name="catalog_update_price_attribute" instance="Magento\Catalog\Observer\SwitchPriceAttributeScopeOnConfigChange" />
5656
</event>
57+
<event name="catalog_product_save_before">
58+
<observer name="set_special_price_start_date" instance="Magento\Catalog\Observer\SetSpecialPriceStartDate" />
59+
</event>
5760
</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
}

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

Lines changed: 10 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>
@@ -194,6 +197,12 @@
194197
<class>\Magento\Ui\Test\Block\Adminhtml\Section</class>
195198
<selector>[data-index='schedule-design-update']</selector>
196199
<strategy>css selector</strategy>
200+
<fields>
201+
<custom_design_from>
202+
<selector>[name="product[custom_design_from]"]</selector>
203+
<strategy>css selector</strategy>
204+
</custom_design_from>
205+
</fields>
197206
</schedule-design-update>
198207
<customer-options>
199208
<class>\Magento\Catalog\Test\Block\Adminhtml\Product\Edit\Section\Options</class>

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

Lines changed: 4 additions & 4 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" />
@@ -81,8 +81,8 @@
8181
<field name="custom_options" is_required="0" group="customer-options" source="Magento\Catalog\Test\Fixture\Product\CustomOptions" repository="Magento\Catalog\Test\Repository\Product\CustomOptions" />
8282
<field name="website_ids" group="websites" source="Magento\Catalog\Test\Fixture\Product\WebsiteIds" />
8383
<field name="is_returnable" is_required="0" group="product-details" />
84-
<field name="news_from_date" is_required="0" source="Magento\Backend\Test\Fixture\Source\Date" />
85-
<field name="news_to_date" is_required="0" source="Magento\Backend\Test\Fixture\Source\Date" />
84+
<field name="news_from_date" is_required="0" group="product-details" source="Magento\Backend\Test\Fixture\Source\Date" />
85+
<field name="news_to_date" is_required="0" group="product-details" source="Magento\Backend\Test\Fixture\Source\Date" />
8686
<field name="stock_data" group="advanced-inventory" />
8787
<field name="checkout_data" group="null" repository="Magento\Catalog\Test\Repository\CatalogProductSimple\CheckoutData" />
8888
<field name="cross_sell_products" group="related" source="Magento\Catalog\Test\Fixture\Product\RelatedProducts" />

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)