Skip to content

Commit

Permalink
MAGETWO-52577: [GitHub] Set Product as New from Date and Design Activ…
Browse files Browse the repository at this point in the history
…e From is set when setting Special Price magento#4387
  • Loading branch information
igrybkov committed Feb 8, 2017
2 parents 2bb7990 + 4147d9d commit a2d59ac
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ protected function _getValueForSave($object)
{
$attributeName = $this->getAttribute()->getName();
$startDate = $object->getData($attributeName);
if ($startDate === false) {
return false;
}
if ($startDate == '' && $object->getSpecialPrice()) {
$startDate = $this->_localeDate->date();
}

return $startDate;
}
Expand Down
45 changes: 45 additions & 0 deletions app/code/Magento/Catalog/Observer/SetSpecialPriceStartDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Observer;

use Magento\Framework\Event\ObserverInterface;

/**
* Set value for Special Price start date
*/
class SetSpecialPriceStartDate implements ObserverInterface
{
/**
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
*/
private $localeDate;

/**
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @codeCoverageIgnore
*/
public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate)
{
$this->localeDate = $localeDate;
}

/**
* Set the current date to Special Price From attribute if it 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()) {
$product->setData('special_from_date', $this->localeDate->date());
}

return $this;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Catalog/etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@
<event name="admin_system_config_changed_section_catalog">
<observer name="catalog_update_price_attribute" instance="Magento\Catalog\Observer\SwitchPriceAttributeScopeOnConfigChange" />
</event>
<event name="catalog_product_save_before">
<observer name="set_special_price_start_date" instance="Magento\Catalog\Observer\SetSpecialPriceStartDate" />
</event>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
<visibility>
<input>select</input>
</visibility>
<news_from_date />
<news_from_date>
<selector>[name="product[news_from_date]"]</selector>
<strategy>css selector</strategy>
</news_from_date>
<news_to_date>
<selector>[name="product[news_to_date]"]</selector>
<strategy>css selector</strategy>
Expand Down Expand Up @@ -194,6 +197,12 @@
<class>\Magento\Ui\Test\Block\Adminhtml\Section</class>
<selector>[data-index='schedule-design-update']</selector>
<strategy>css selector</strategy>
<fields>
<custom_design_from>
<selector>[name="product[custom_design_from]"]</selector>
<strategy>css selector</strategy>
</custom_design_from>
</fields>
</schedule-design-update>
<customer-options>
<class>\Magento\Catalog\Test\Block\Adminhtml\Product\Edit\Section\Options</class>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
<field name="country_of_manufacture" is_required="0" />
<field name="created_at" is_required="1" />
<field name="custom_design" is_required="0" />
<field name="custom_design_from" is_required="0" />
<field name="custom_design_to" is_required="0" />
<field name="custom_design_from" is_required="0" group="schedule-design-update" source="Magento\Backend\Test\Fixture\Source\Date" />
<field name="custom_design_to" is_required="0" group="schedule-design-update" source="Magento\Backend\Test\Fixture\Source\Date" />
<field name="custom_layout_update" is_required="0" />
<field name="description" is_required="0" group="content" />
<field name="gallery" is_required="0" />
Expand Down Expand Up @@ -81,8 +81,8 @@
<field name="custom_options" is_required="0" group="customer-options" source="Magento\Catalog\Test\Fixture\Product\CustomOptions" repository="Magento\Catalog\Test\Repository\Product\CustomOptions" />
<field name="website_ids" group="websites" source="Magento\Catalog\Test\Fixture\Product\WebsiteIds" />
<field name="is_returnable" is_required="0" group="product-details" />
<field name="news_from_date" is_required="0" source="Magento\Backend\Test\Fixture\Source\Date" />
<field name="news_to_date" is_required="0" source="Magento\Backend\Test\Fixture\Source\Date" />
<field name="news_from_date" is_required="0" group="product-details" source="Magento\Backend\Test\Fixture\Source\Date" />
<field name="news_to_date" is_required="0" group="product-details" source="Magento\Backend\Test\Fixture\Source\Date" />
<field name="stock_data" group="advanced-inventory" />
<field name="checkout_data" group="null" repository="Magento\Catalog\Test\Repository\CatalogProductSimple\CheckoutData" />
<field name="cross_sell_products" group="related" source="Magento\Catalog\Test\Fixture\Product\RelatedProducts" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@
<data name="product/data/short_description" xsi:type="string">Simple Product short_description %isolation%</data>
<data name="product/data/description" xsi:type="string">Simple Product description %isolation%</data>
<data name="product/data/weight" xsi:type="string">52</data>
<data name="product/data/news_from_date" xsi:type="string"></data>
<data name="product/data/custom_design_from" xsi:type="string"></data>
<data name="product/data/quantity_and_stock_status/qty" xsi:type="string">659</data>
<data name="product/data/custom_options/dataset" xsi:type="string">drop_down_with_one_option_fixed_price</data>
<data name="product/data/checkout_data/dataset" xsi:type="string">simple_drop_down_with_one_option_fixed_price</data>
<data name="product/data/price/dataset" xsi:type="string">MAGETWO-23029</data>
<constraint name="Magento\Catalog\Test\Constraint\AssertProductSaveMessage" />
<constraint name="Magento\Catalog\Test\Constraint\AssertProductForm" />
<constraint name="Magento\Catalog\Test\Constraint\AssertProductInGrid" />
<constraint name="Magento\Catalog\Test\Constraint\AssertProductInCategory" />
<constraint name="Magento\Catalog\Test\Constraint\AssertProductPage" />
Expand Down

0 comments on commit a2d59ac

Please sign in to comment.