Skip to content

Commit 87274f3

Browse files
committed
MAGETWO-55791: Merge branch '2.0-develop' of github.com:magento/magento2ce into MAGETWO-55791
2 parents 00b32bd + 90e0f4c commit 87274f3

File tree

4 files changed

+84
-15
lines changed

4 files changed

+84
-15
lines changed

app/code/Magento/CatalogImportExport/Model/Export/Product.php

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\CatalogImportExport\Model\Export;
77

8+
use Magento\Framework\DB\Ddl\Table;
89
use Magento\ImportExport\Model\Import;
910
use \Magento\Store\Model\Store;
1011
use \Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
@@ -88,6 +89,13 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
8889
'gift_message_available',
8990
];
9091

92+
/**
93+
* Attributes defined by user
94+
*
95+
* @var array
96+
*/
97+
private $userDefinedAttributes = [];
98+
9199
/**
92100
* @var array
93101
*/
@@ -255,6 +263,20 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
255263
'tax_class_id' => 'tax_class_name',
256264
];
257265

266+
/**
267+
* Codes of attributes which are displayed as dates
268+
*
269+
* @var array
270+
*/
271+
protected $dateAttrCodes = [
272+
'special_from_date',
273+
'special_to_date',
274+
'news_from_date',
275+
'news_to_date',
276+
'custom_design_from',
277+
'custom_design_to'
278+
];
279+
258280
/**
259281
* Attributes codes which are appropriate for export and not the part of additional_attributes.
260282
*
@@ -327,6 +349,7 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
327349
* @param Product\Type\Factory $_typeFactory
328350
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
329351
* @param \Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer
352+
* @param array $dateAttrCodes
330353
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
331354
*/
332355
public function __construct(
@@ -345,7 +368,8 @@ public function __construct(
345368
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeColFactory,
346369
\Magento\CatalogImportExport\Model\Export\Product\Type\Factory $_typeFactory,
347370
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider,
348-
\Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer
371+
\Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer,
372+
array $dateAttrCodes = []
349373
) {
350374
$this->_entityCollectionFactory = $collectionFactory;
351375
$this->_exportConfig = $exportConfig;
@@ -360,6 +384,7 @@ public function __construct(
360384
$this->_typeFactory = $_typeFactory;
361385
$this->_linkTypeProvider = $linkTypeProvider;
362386
$this->rowCustomizer = $rowCustomizer;
387+
$this->dateAttrCodes = array_merge($this->dateAttrCodes, $dateAttrCodes);
363388

364389
parent::__construct($localeDate, $config, $resource, $storeManager);
365390

@@ -875,12 +900,24 @@ protected function collectRawData()
875900
}
876901
$fieldName = isset($this->_fieldsMap[$code]) ? $this->_fieldsMap[$code] : $code;
877902

878-
if ($this->_attributeTypes[$code] === 'datetime') {
879-
$attrValue = $this->_localeDate->formatDateTime(
880-
new \DateTime($attrValue),
881-
\IntlDateFormatter::SHORT,
882-
\IntlDateFormatter::SHORT
883-
);
903+
if ($this->_attributeTypes[$code] == 'datetime') {
904+
if (in_array($code, $this->dateAttrCodes)
905+
|| in_array($code, $this->userDefinedAttributes)
906+
) {
907+
$attrValue = $this->_localeDate->formatDateTime(
908+
new \DateTime($attrValue),
909+
\IntlDateFormatter::SHORT,
910+
\IntlDateFormatter::NONE,
911+
null,
912+
date_default_timezone_get()
913+
);
914+
} else {
915+
$attrValue = $this->_localeDate->formatDateTime(
916+
new \DateTime($attrValue),
917+
\IntlDateFormatter::SHORT,
918+
\IntlDateFormatter::SHORT
919+
);
920+
}
884921
}
885922

886923
if ($storeId != Store::DEFAULT_STORE_ID
@@ -1327,6 +1364,9 @@ protected function initAttributes()
13271364
$this->_attributeValues[$attribute->getAttributeCode()] = $this->getAttributeOptions($attribute);
13281365
$this->_attributeTypes[$attribute->getAttributeCode()] =
13291366
\Magento\ImportExport\Model\Import::getAttributeType($attribute);
1367+
if ($attribute->getIsUserDefined()) {
1368+
$this->userDefinedAttributes[] = $attribute->getAttributeCode();
1369+
}
13301370
}
13311371
return $this;
13321372
}

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,20 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
205205
'_upsell_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_UPSELL,
206206
];
207207

208+
/**
209+
* Codes of attributes which are displayed as dates
210+
*
211+
* @var array
212+
*/
213+
protected $dateAttrCodes = [
214+
'special_from_date',
215+
'special_to_date',
216+
'news_from_date',
217+
'news_to_date',
218+
'custom_design_from',
219+
'custom_design_to'
220+
];
221+
208222
/**
209223
* Need to log in import history
210224
*
@@ -649,6 +663,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
649663
* @param Product\TaxClassProcessor $taxClassProcessor
650664
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
651665
* @param array $data
666+
* @param array $dateAttrCodes
652667
* @throws \Magento\Framework\Exception\LocalizedException
653668
*
654669
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -690,7 +705,8 @@ public function __construct(
690705
Product\TaxClassProcessor $taxClassProcessor,
691706
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
692707
\Magento\Catalog\Model\Product\Url $productUrl,
693-
array $data = []
708+
array $data = [],
709+
array $dateAttrCodes = []
694710
) {
695711
$this->_eventManager = $eventManager;
696712
$this->stockRegistry = $stockRegistry;
@@ -719,6 +735,7 @@ public function __construct(
719735
$this->taxClassProcessor = $taxClassProcessor;
720736
$this->scopeConfig = $scopeConfig;
721737
$this->productUrl = $productUrl;
738+
$this->dateAttrCodes = array_merge($this->dateAttrCodes, $dateAttrCodes);
722739
parent::__construct(
723740
$jsonHelper,
724741
$importExportData,
@@ -1645,7 +1662,15 @@ protected function _saveProducts()
16451662
$attrTable = $attribute->getBackend()->getTable();
16461663
$storeIds = [0];
16471664

1648-
if ('datetime' == $attribute->getBackendType() && strtotime($attrValue)) {
1665+
if (
1666+
'datetime' == $attribute->getBackendType()
1667+
&& (
1668+
in_array($attribute->getAttributeCode(), $this->dateAttrCodes)
1669+
|| $attribute->getIsUserDefined()
1670+
)
1671+
) {
1672+
$attrValue = $this->dateTime->formatDate($attrValue, false);
1673+
} else if ('datetime' == $attribute->getBackendType() && strtotime($attrValue)) {
16491674
$attrValue = $this->dateTime->gmDate(
16501675
'Y-m-d H:i:s',
16511676
$this->_localeDate->date($attrValue)->getTimestamp()

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,12 @@ public function testSaveDatetimeAttribute()
362362
);
363363
$productAfterImport->load($productBeforeImport->getId());
364364
$this->assertEquals(
365-
@strtotime($row['news_from_date']),
366-
@strtotime($productAfterImport->getNewsFromDate())
365+
@strtotime(date('m/d/Y', @strtotime($row['news_from_date']))),
366+
@strtotime($productAfterImport->getNewsFromDate())
367+
);
368+
$this->assertEquals(
369+
@strtotime($row['news_to_date']),
370+
@strtotime($productAfterImport->getNewsToDate())
367371
);
368372
unset($productAfterImport);
369373
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
sku,news_from_date
2-
simple1,"1/1/2015 20:00"
3-
simple2,"10/8/2012 23:58"
4-
simple3,"12/31/1998 17:30"
1+
sku,news_from_date,news_to_date
2+
simple1,"1/1/2015 20:00","1/2/2015"
3+
simple2,"10/8/2012 23:58","1/3/2015"
4+
simple3,"12/31/1998 17:30","1/4/2015"

0 commit comments

Comments
 (0)