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 #4387
  • Loading branch information
miakusha committed Jan 11, 2017
1 parent bbb180f commit a24a47f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ protected function _getValueForSave($object)
if ($startDate === false) {
return false;
}
if ($startDate == '' && $object->getSpecialPrice()) {
$startDate = $this->_localeDate->date();
}

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

use Magento\Framework\Event\ObserverInterface;

class SetSpecialPriceStartDateObserver implements ObserverInterface
{
/**
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
*/
protected $localeDate;

/**
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @codeCoverageIgnore
*/
public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate)
{
$this->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;
}
}
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 @@ -51,4 +51,7 @@
<event name="magento_catalog_api_data_categorytreeinterface_load_after">
<observer name="legacy_categorytree_load_after" instance="Magento\Framework\EntityManager\Observer\AfterEntityLoad" />
</event>
<event name="catalog_product_save_before">
<observer name="set_special_price_start_date" instance="Magento\Catalog\Observer\SetSpecialPriceStartDateObserver" />
</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)));
}
}

0 comments on commit a24a47f

Please sign in to comment.