Skip to content

Commit

Permalink
Merge pull request #553 from magento-okapis
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Oleksii Korshenko authored Nov 1, 2016
2 parents 8a66196 + 43842ea commit 92d8e3d
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 45 deletions.
54 changes: 47 additions & 7 deletions app/code/Magento/CatalogImportExport/Model/Export/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\CatalogImportExport\Model\Export;

use Magento\Framework\DB\Ddl\Table;
use Magento\ImportExport\Model\Import;
use \Magento\Store\Model\Store;
use \Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
Expand Down Expand Up @@ -87,6 +88,13 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
'status',
];

/**
* Attributes defined by user
*
* @var array
*/
private $userDefinedAttributes = [];

/**
* @var array
*/
Expand Down Expand Up @@ -254,6 +262,20 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
'tax_class_id' => 'tax_class_name',
];

/**
* Codes of attributes which are displayed as dates
*
* @var array
*/
protected $dateAttrCodes = [
'special_from_date',
'special_to_date',
'news_from_date',
'news_to_date',
'custom_design_from',
'custom_design_to'
];

/**
* Attributes codes which are appropriate for export and not the part of additional_attributes.
*
Expand Down Expand Up @@ -338,6 +360,7 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
* @param Product\Type\Factory $_typeFactory
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
* @param \Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer
* @param array $dateAttrCodes
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -356,7 +379,8 @@ public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeColFactory,
\Magento\CatalogImportExport\Model\Export\Product\Type\Factory $_typeFactory,
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider,
\Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer
\Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer,
array $dateAttrCodes = []
) {
$this->_entityCollectionFactory = $collectionFactory;
$this->_exportConfig = $exportConfig;
Expand All @@ -371,6 +395,7 @@ public function __construct(
$this->_typeFactory = $_typeFactory;
$this->_linkTypeProvider = $linkTypeProvider;
$this->rowCustomizer = $rowCustomizer;
$this->dateAttrCodes = array_merge($this->dateAttrCodes, $dateAttrCodes);

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

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

if ($this->_attributeTypes[$code] === 'datetime') {
$attrValue = $this->_localeDate->formatDateTime(
new \DateTime($attrValue),
\IntlDateFormatter::SHORT,
\IntlDateFormatter::SHORT
);
if ($this->_attributeTypes[$code] == 'datetime') {
if (in_array($code, $this->dateAttrCodes)
|| in_array($code, $this->userDefinedAttributes)
) {
$attrValue = $this->_localeDate->formatDateTime(
new \DateTime($attrValue),
\IntlDateFormatter::SHORT,
\IntlDateFormatter::NONE,
null,
date_default_timezone_get()
);
} else {
$attrValue = $this->_localeDate->formatDateTime(
new \DateTime($attrValue),
\IntlDateFormatter::SHORT,
\IntlDateFormatter::SHORT
);
}
}

if ($storeId != Store::DEFAULT_STORE_ID
Expand Down Expand Up @@ -1370,6 +1407,9 @@ protected function initAttributes()
$this->_attributeValues[$attribute->getAttributeCode()] = $this->getAttributeOptions($attribute);
$this->_attributeTypes[$attribute->getAttributeCode()] =
\Magento\ImportExport\Model\Import::getAttributeType($attribute);
if ($attribute->getIsUserDefined()) {
$this->userDefinedAttributes[] = $attribute->getAttributeCode();
}
}
return $this;
}
Expand Down
29 changes: 27 additions & 2 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
'_upsell_' => \Magento\Catalog\Model\Product\Link::LINK_TYPE_UPSELL,
];

/**
* Codes of attributes which are displayed as dates
*
* @var array
*/
protected $dateAttrCodes = [
'special_from_date',
'special_to_date',
'news_from_date',
'news_to_date',
'custom_design_from',
'custom_design_to'
];

/**
* Need to log in import history
*
Expand Down Expand Up @@ -670,6 +684,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
* @param Product\TaxClassProcessor $taxClassProcessor
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param array $data
* @param array $dateAttrCodes
* @throws \Magento\Framework\Exception\LocalizedException
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand Down Expand Up @@ -711,7 +726,8 @@ public function __construct(
Product\TaxClassProcessor $taxClassProcessor,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Catalog\Model\Product\Url $productUrl,
array $data = []
array $data = [],
array $dateAttrCodes = []
) {
$this->_eventManager = $eventManager;
$this->stockRegistry = $stockRegistry;
Expand Down Expand Up @@ -740,6 +756,7 @@ public function __construct(
$this->taxClassProcessor = $taxClassProcessor;
$this->scopeConfig = $scopeConfig;
$this->productUrl = $productUrl;
$this->dateAttrCodes = array_merge($this->dateAttrCodes, $dateAttrCodes);
parent::__construct(
$jsonHelper,
$importExportData,
Expand Down Expand Up @@ -1683,7 +1700,15 @@ protected function _saveProducts()
$attrTable = $attribute->getBackend()->getTable();
$storeIds = [0];

if ('datetime' == $attribute->getBackendType() && strtotime($attrValue)) {
if (
'datetime' == $attribute->getBackendType()
&& (
in_array($attribute->getAttributeCode(), $this->dateAttrCodes)
|| $attribute->getIsUserDefined()
)
) {
$attrValue = $this->dateTime->formatDate($attrValue, false);
} else if ('datetime' == $attribute->getBackendType() && strtotime($attrValue)) {
$attrValue = $this->dateTime->gmDate(
'Y-m-d H:i:s',
$this->_localeDate->date($attrValue)->getTimestamp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,7 @@ protected function setUp()
$this->attributeColFactory,
$this->typeFactory,
$this->linkTypeProvider,
$this->rowCustomizer,
$this->metadataPool
$this->rowCustomizer
);

$this->object = new StubProduct();
Expand Down
77 changes: 77 additions & 0 deletions app/code/Magento/CatalogInventory/Setup/UpgradeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogInventory\Setup;

use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\Framework\Indexer\AbstractProcessor;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Upgrade Data script
* @codeCoverageIgnore
*/
class UpgradeData implements UpgradeDataInterface
{
/**
* @var StockConfigurationInterface
*/
private $configuration;

/**
* @var AbstractProcessor
*/
private $indexerProcessor;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param StockConfigurationInterface $configuration
* @param StoreManagerInterface $storeManager
* @param AbstractProcessor $indexerProcessor
*/
public function __construct(
StockConfigurationInterface $configuration,
StoreManagerInterface $storeManager,
AbstractProcessor $indexerProcessor
) {
$this->configuration = $configuration;
$this->storeManager = $storeManager;
$this->indexerProcessor = $indexerProcessor;
}

/**
* {@inheritdoc}
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if (version_compare($context->getVersion(), '2.0.1') < 0)
{
$this->upgradeCatalogInventoryStockItem($setup);
}
$setup->endSetup();
}

/**
* @param ModuleDataSetupInterface $setup
* @return void
*/
private function upgradeCatalogInventoryStockItem($setup)
{
$setup->getConnection()->update(
$setup->getTable('cataloginventory_stock_item'),
['website_id' => $this->configuration->getDefaultScopeId()],
['website_id = ?' => $this->storeManager->getWebsite()->getId()]
);
$this->indexerProcessor->getIndexer()->invalidate();
}
}
5 changes: 5 additions & 0 deletions app/code/Magento/CatalogInventory/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,9 @@
</argument>
</arguments>
</type>
<type name="Magento\CatalogInventory\Setup\UpgradeData">
<arguments>
<argument name="indexerProcessor" xsi:type="object">Magento\CatalogInventory\Model\Indexer\Stock\Processor</argument>
</arguments>
</type>
</config>
2 changes: 1 addition & 1 deletion app/code/Magento/CatalogInventory/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_CatalogInventory" setup_version="2.0.0">
<module name="Magento_CatalogInventory" setup_version="2.0.1">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "magento/magento2ce",
"description": "Magento 2 (Community Edition)",
"type": "project",
"version": "2.1.3-dev",
"version": "2.1.3-rc",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
Loading

0 comments on commit 92d8e3d

Please sign in to comment.