Skip to content

Commit 8d6cfc3

Browse files
author
Robert He
committed
MAGETWO-56804: [Backport] Product catalog Import/export - Date & Timezone issue - for 2.1
- backport changes to fix timezone issues with import/export
1 parent 8a66196 commit 8d6cfc3

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;
@@ -87,6 +88,13 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
8788
'status',
8889
];
8990

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

265+
/**
266+
* Codes of attributes which are displayed as dates
267+
*
268+
* @var array
269+
*/
270+
protected $dateAttrCodes = [
271+
'special_from_date',
272+
'special_to_date',
273+
'news_from_date',
274+
'news_to_date',
275+
'custom_design_from',
276+
'custom_design_to'
277+
];
278+
257279
/**
258280
* Attributes codes which are appropriate for export and not the part of additional_attributes.
259281
*
@@ -338,6 +360,7 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
338360
* @param Product\Type\Factory $_typeFactory
339361
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
340362
* @param \Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer
363+
* @param array $dateAttrCodes
341364
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
342365
*/
343366
public function __construct(
@@ -356,7 +379,8 @@ public function __construct(
356379
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeColFactory,
357380
\Magento\CatalogImportExport\Model\Export\Product\Type\Factory $_typeFactory,
358381
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider,
359-
\Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer
382+
\Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer,
383+
array $dateAttrCodes = []
360384
) {
361385
$this->_entityCollectionFactory = $collectionFactory;
362386
$this->_exportConfig = $exportConfig;
@@ -371,6 +395,7 @@ public function __construct(
371395
$this->_typeFactory = $_typeFactory;
372396
$this->_linkTypeProvider = $linkTypeProvider;
373397
$this->rowCustomizer = $rowCustomizer;
398+
$this->dateAttrCodes = array_merge($this->dateAttrCodes, $dateAttrCodes);
374399

375400
parent::__construct($localeDate, $config, $resource, $storeManager);
376401

@@ -897,12 +922,24 @@ protected function collectRawData()
897922
}
898923
$fieldName = isset($this->_fieldsMap[$code]) ? $this->_fieldsMap[$code] : $code;
899924

900-
if ($this->_attributeTypes[$code] === 'datetime') {
901-
$attrValue = $this->_localeDate->formatDateTime(
902-
new \DateTime($attrValue),
903-
\IntlDateFormatter::SHORT,
904-
\IntlDateFormatter::SHORT
905-
);
925+
if ($this->_attributeTypes[$code] == 'datetime') {
926+
if (in_array($code, $this->dateAttrCodes)
927+
|| in_array($code, $this->userDefinedAttributes)
928+
) {
929+
$attrValue = $this->_localeDate->formatDateTime(
930+
new \DateTime($attrValue),
931+
\IntlDateFormatter::SHORT,
932+
\IntlDateFormatter::NONE,
933+
null,
934+
date_default_timezone_get()
935+
);
936+
} else {
937+
$attrValue = $this->_localeDate->formatDateTime(
938+
new \DateTime($attrValue),
939+
\IntlDateFormatter::SHORT,
940+
\IntlDateFormatter::SHORT
941+
);
942+
}
906943
}
907944

908945
if ($storeId != Store::DEFAULT_STORE_ID
@@ -1370,6 +1407,9 @@ protected function initAttributes()
13701407
$this->_attributeValues[$attribute->getAttributeCode()] = $this->getAttributeOptions($attribute);
13711408
$this->_attributeTypes[$attribute->getAttributeCode()] =
13721409
\Magento\ImportExport\Model\Import::getAttributeType($attribute);
1410+
if ($attribute->getIsUserDefined()) {
1411+
$this->userDefinedAttributes[] = $attribute->getAttributeCode();
1412+
}
13731413
}
13741414
return $this;
13751415
}

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

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

212+
/**
213+
* Codes of attributes which are displayed as dates
214+
*
215+
* @var array
216+
*/
217+
protected $dateAttrCodes = [
218+
'special_from_date',
219+
'special_to_date',
220+
'news_from_date',
221+
'news_to_date',
222+
'custom_design_from',
223+
'custom_design_to'
224+
];
225+
212226
/**
213227
* Need to log in import history
214228
*
@@ -670,6 +684,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
670684
* @param Product\TaxClassProcessor $taxClassProcessor
671685
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
672686
* @param array $data
687+
* @param array $dateAttrCodes
673688
* @throws \Magento\Framework\Exception\LocalizedException
674689
*
675690
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -711,7 +726,8 @@ public function __construct(
711726
Product\TaxClassProcessor $taxClassProcessor,
712727
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
713728
\Magento\Catalog\Model\Product\Url $productUrl,
714-
array $data = []
729+
array $data = [],
730+
array $dateAttrCodes = []
715731
) {
716732
$this->_eventManager = $eventManager;
717733
$this->stockRegistry = $stockRegistry;
@@ -740,6 +756,7 @@ public function __construct(
740756
$this->taxClassProcessor = $taxClassProcessor;
741757
$this->scopeConfig = $scopeConfig;
742758
$this->productUrl = $productUrl;
759+
$this->dateAttrCodes = array_merge($this->dateAttrCodes, $dateAttrCodes);
743760
parent::__construct(
744761
$jsonHelper,
745762
$importExportData,
@@ -1683,7 +1700,15 @@ protected function _saveProducts()
16831700
$attrTable = $attribute->getBackend()->getTable();
16841701
$storeIds = [0];
16851702

1686-
if ('datetime' == $attribute->getBackendType() && strtotime($attrValue)) {
1703+
if (
1704+
'datetime' == $attribute->getBackendType()
1705+
&& (
1706+
in_array($attribute->getAttributeCode(), $this->dateAttrCodes)
1707+
|| $attribute->getIsUserDefined()
1708+
)
1709+
) {
1710+
$attrValue = $this->dateTime->formatDate($attrValue, false);
1711+
} else if ('datetime' == $attribute->getBackendType() && strtotime($attrValue)) {
16871712
$attrValue = $this->dateTime->gmDate(
16881713
'Y-m-d H:i:s',
16891714
$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
@@ -388,8 +388,12 @@ public function testSaveDatetimeAttribute()
388388
);
389389
$productAfterImport->load($productBeforeImport->getId());
390390
$this->assertEquals(
391-
@strtotime($row['news_from_date']),
392-
@strtotime($productAfterImport->getNewsFromDate())
391+
@strtotime(date('m/d/Y', @strtotime($row['news_from_date']))),
392+
@strtotime($productAfterImport->getNewsFromDate())
393+
);
394+
$this->assertEquals(
395+
@strtotime($row['news_to_date']),
396+
@strtotime($productAfterImport->getNewsToDate())
393397
);
394398
unset($productAfterImport);
395399
}
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)