Skip to content

Commit 92d8e3d

Browse files
author
Oleksii Korshenko
authored
Merge pull request #553 from magento-okapis
Fixed issue: - MAGETWO-56804 [Backport] Product catalog Import/export - Date & Timezone issue - for 2.1 - MAGETWO-58312 [Backport][GitHub] Products became “out of stock” after update from 2.0.7 to 2.1.0 #5222 - for 2.1
2 parents 8a66196 + 43842ea commit 92d8e3d

File tree

10 files changed

+195
-45
lines changed

10 files changed

+195
-45
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()

app/code/Magento/CatalogImportExport/Test/Unit/Model/Export/ProductTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ protected function setUp()
343343
$this->attributeColFactory,
344344
$this->typeFactory,
345345
$this->linkTypeProvider,
346-
$this->rowCustomizer,
347-
$this->metadataPool
346+
$this->rowCustomizer
348347
);
349348

350349
$this->object = new StubProduct();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogInventory\Setup;
7+
8+
use Magento\CatalogInventory\Api\StockConfigurationInterface;
9+
use Magento\Framework\Indexer\AbstractProcessor;
10+
use Magento\Framework\Setup\UpgradeDataInterface;
11+
use Magento\Framework\Setup\ModuleContextInterface;
12+
use Magento\Framework\Setup\ModuleDataSetupInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
15+
/**
16+
* Upgrade Data script
17+
* @codeCoverageIgnore
18+
*/
19+
class UpgradeData implements UpgradeDataInterface
20+
{
21+
/**
22+
* @var StockConfigurationInterface
23+
*/
24+
private $configuration;
25+
26+
/**
27+
* @var AbstractProcessor
28+
*/
29+
private $indexerProcessor;
30+
31+
/**
32+
* @var StoreManagerInterface
33+
*/
34+
private $storeManager;
35+
36+
/**
37+
* @param StockConfigurationInterface $configuration
38+
* @param StoreManagerInterface $storeManager
39+
* @param AbstractProcessor $indexerProcessor
40+
*/
41+
public function __construct(
42+
StockConfigurationInterface $configuration,
43+
StoreManagerInterface $storeManager,
44+
AbstractProcessor $indexerProcessor
45+
) {
46+
$this->configuration = $configuration;
47+
$this->storeManager = $storeManager;
48+
$this->indexerProcessor = $indexerProcessor;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
55+
{
56+
$setup->startSetup();
57+
if (version_compare($context->getVersion(), '2.0.1') < 0)
58+
{
59+
$this->upgradeCatalogInventoryStockItem($setup);
60+
}
61+
$setup->endSetup();
62+
}
63+
64+
/**
65+
* @param ModuleDataSetupInterface $setup
66+
* @return void
67+
*/
68+
private function upgradeCatalogInventoryStockItem($setup)
69+
{
70+
$setup->getConnection()->update(
71+
$setup->getTable('cataloginventory_stock_item'),
72+
['website_id' => $this->configuration->getDefaultScopeId()],
73+
['website_id = ?' => $this->storeManager->getWebsite()->getId()]
74+
);
75+
$this->indexerProcessor->getIndexer()->invalidate();
76+
}
77+
}

app/code/Magento/CatalogInventory/etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,9 @@
8181
</argument>
8282
</arguments>
8383
</type>
84+
<type name="Magento\CatalogInventory\Setup\UpgradeData">
85+
<arguments>
86+
<argument name="indexerProcessor" xsi:type="object">Magento\CatalogInventory\Model\Indexer\Stock\Processor</argument>
87+
</arguments>
88+
</type>
8489
</config>

app/code/Magento/CatalogInventory/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9-
<module name="Magento_CatalogInventory" setup_version="2.0.0">
9+
<module name="Magento_CatalogInventory" setup_version="2.0.1">
1010
<sequence>
1111
<module name="Magento_Catalog"/>
1212
</sequence>

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "magento/magento2ce",
33
"description": "Magento 2 (Community Edition)",
44
"type": "project",
5-
"version": "2.1.3-dev",
5+
"version": "2.1.3-rc",
66
"license": [
77
"OSL-3.0",
88
"AFL-3.0"

0 commit comments

Comments
 (0)