Skip to content

Commit 50572ed

Browse files
author
Robert He
committed
MAGETWO-56803: [Backport] Product catalog Import/export - Date & Timezone issue - for 2.0
- backport changes to fix timezone issues with import/export
1 parent 6f97f01 commit 50572ed

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+
* Attributes codes which shows as date
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
@@ -1308,6 +1345,9 @@ protected function initAttributes()
13081345
$this->_attributeValues[$attribute->getAttributeCode()] = $this->getAttributeOptions($attribute);
13091346
$this->_attributeTypes[$attribute->getAttributeCode()] =
13101347
\Magento\ImportExport\Model\Import::getAttributeType($attribute);
1348+
if ($attribute->getIsUserDefined()) {
1349+
$this->userDefinedAttributes[] = $attribute->getAttributeCode();
1350+
}
13111351
}
13121352
return $this;
13131353
}

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+
* Attributes codes which shows as date
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
*
@@ -642,6 +656,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
642656
* @param Product\TaxClassProcessor $taxClassProcessor
643657
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
644658
* @param array $data
659+
* @param array $dateAttrCodes
645660
* @throws \Magento\Framework\Exception\LocalizedException
646661
*
647662
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -683,7 +698,8 @@ public function __construct(
683698
Product\TaxClassProcessor $taxClassProcessor,
684699
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
685700
\Magento\Catalog\Model\Product\Url $productUrl,
686-
array $data = []
701+
array $data = [],
702+
array $dateAttrCodes = []
687703
) {
688704
$this->_eventManager = $eventManager;
689705
$this->stockRegistry = $stockRegistry;
@@ -712,6 +728,7 @@ public function __construct(
712728
$this->taxClassProcessor = $taxClassProcessor;
713729
$this->scopeConfig = $scopeConfig;
714730
$this->productUrl = $productUrl;
731+
$this->dateAttrCodes = array_merge($this->dateAttrCodes, $dateAttrCodes);
715732
parent::__construct(
716733
$jsonHelper,
717734
$importExportData,
@@ -1638,7 +1655,15 @@ protected function _saveProducts()
16381655
$attrTable = $attribute->getBackend()->getTable();
16391656
$storeIds = [0];
16401657

1641-
if ('datetime' == $attribute->getBackendType() && strtotime($attrValue)) {
1658+
if (
1659+
'datetime' == $attribute->getBackendType()
1660+
&& (
1661+
in_array($attribute->getAttributeCode(), $this->dateAttrCodes)
1662+
|| $attribute->getIsUserDefined()
1663+
)
1664+
) {
1665+
$attrValue = $this->dateTime->formatDate($attrValue, false);
1666+
} else if ('datetime' == $attribute->getBackendType() && strtotime($attrValue)) {
16421667
$attrValue = $this->dateTime->gmDate(
16431668
'Y-m-d H:i:s',
16441669
$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)