Skip to content

Various fixes in Magento 2 core to support MSI #13434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
992bb67
MSI: update Magento 2 core to support MSI
vrann Jan 31, 2018
420afc8
Some fixes
p-bystritsky Mar 13, 2018
13efce8
MSI: 496: Provide StockImporter implementation for Magento CE
RomaKis Mar 13, 2018
9573d88
Merge remote-tracking branch 'magento2/2.3-develop' into msi-core-cha…
RomaKis Mar 13, 2018
0067244
MSI: Update Magento 2 core to support MSI
RomaKis Mar 13, 2018
ad91424
Merge remote-tracking branch 'magento2/2.3-develop' into msi-core-cha…
RomaKis Mar 13, 2018
1b8cc6a
MSI: Update Magento 2 core to support MSI
RomaKis Mar 13, 2018
36098f4
MSI: Update Magento 2 core to support MSI
RomaKis Mar 13, 2018
f016278
MSI: Update Magento 2 core to support MSI
RomaKis Mar 13, 2018
bffe557
MSI: Update Magento 2 core to support MSI
RomaKis Mar 15, 2018
d4e8e3b
Merge remote-tracking branch 'origin/msi-core-changes-no-history' int…
RomaKis Mar 15, 2018
6d5e882
MSI: Update Magento 2 core to support MSI
RomaKis Mar 15, 2018
c084214
MSI: Update Magento 2 core to support MSI
RomaKis Mar 16, 2018
3a2533b
MSI: Update Magento 2 core to support MSI
RomaKis Mar 16, 2018
6a3835f
Remove unique key for (product_id, website_id) on cataloginventory_st…
maghamed Mar 17, 2018
0a49140
Merge remote-tracking branch 'magento2/2.3-develop' into msi-core-cha…
RomaKis Mar 19, 2018
aba9b73
MSI: Update Magento 2 core to support MSI
RomaKis Mar 19, 2018
7fc59e7
Merge remote-tracking branch 'origin/msi-core-changes-no-history' int…
RomaKis Mar 19, 2018
661541a
get back declaration of CATALOGINVENTORY_STOCK_ITEM_PRODUCT_ID_WEBSIT…
maghamed Mar 19, 2018
bb6bac3
MSI: Update Magento 2 core to support MSI
RomaKis Mar 20, 2018
6f22a17
Merge remote-tracking branch 'magento2/2.3-develop' into msi-core-cha…
RomaKis Mar 20, 2018
e4eab13
Merge remote-tracking branch 'origin/msi-core-changes-no-history' int…
RomaKis Mar 20, 2018
28b07ed
MSI: Update Magento 2 core to support MSI
RomaKis Mar 22, 2018
a509c71
Merge remote-tracking branch 'magento2/2.3-develop' into msi-core-cha…
RomaKis Mar 22, 2018
be29aa7
MSI: Update Magento 2 core to support MSI
RomaKis Mar 22, 2018
aa365e0
Merge remote-tracking branch 'mainline/2.3-develop' into msi-core-cha…
Mar 26, 2018
af7737c
magento/magento2#13434: Fix strict type checking for legacy code
Mar 26, 2018
e949bdb
magento/magento2#13434: Strict type static test should be preformed o…
Mar 27, 2018
0dceea8
Remove Support of PHP 7.0.* on Travis Buils
maghamed Mar 27, 2018
d62db82
Merge remote-tracking branch 'mainline/2.3-develop' into msi-core-cha…
Mar 27, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions app/code/Magento/Backend/Ui/Component/Control/DeleteButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Backend\Ui\Component\Control;

use Magento\Framework\App\RequestInterface;
use Magento\Framework\Escaper;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

/**
* Represents delete button with pre-configured options
* Provide an ability to show confirmation message on click on the "Delete" button
*
* @api
*/
class DeleteButton implements ButtonProviderInterface
{
/**
* @var RequestInterface
*/
private $request;

/**
* @var UrlInterface
*/
private $urlBuilder;

/**
* @var Escaper
*/
private $escaper;

/**
* @var string
*/
private $confirmationMessage;

/**
* @var string
*/
private $idFieldName;

/**
* @var string
*/
private $deleteRoutePath;

/**
* @var int
*/
private $sortOrder;

/**
* @param RequestInterface $request
* @param UrlInterface $urlBuilder
* @param string $confirmationMessage
* @param string $idFieldName
* @param string $deleteRoutePath
* @param int $sortOrder
*/
public function __construct(
RequestInterface $request,
UrlInterface $urlBuilder,
Escaper $escaper,
string $confirmationMessage,
string $idFieldName,
string $deleteRoutePath,
int $sortOrder
) {
$this->request = $request;
$this->urlBuilder = $urlBuilder;
$this->escaper = $escaper;
$this->confirmationMessage = $confirmationMessage;
$this->idFieldName = $idFieldName;
$this->deleteRoutePath = $deleteRoutePath;
$this->sortOrder = $sortOrder;
}

/**
* {@inheritdoc}
*/
public function getButtonData()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class seems to be unused in the current implementation.
I would suggest delivering it with the code which actually uses it and provides functional test coverage or with unit/integration test which would ensure that it is not broken in the future.

{
$data = [];
$fieldId = $this->request->getParam($this->idFieldName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, we need to escape $fieldId
Because this value we obtain from request

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if (null !== $fieldId) {
$url = $this->urlBuilder->getUrl($this->deleteRoutePath);
$escapedMessage = $this->escaper->escapeJs($this->escaper->escapeHtml($this->confirmationMessage));
$data = [
'label' => __('Delete'),
'class' => 'delete',
'on_click' => "deleteConfirm('{$escapedMessage}', '{$url}', {data:{{$this->idFieldName}:{$fieldId}}})",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential XSS via $fieldId

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

'sort_order' => $this->sortOrder,
];
}
return $data;
}
}
22 changes: 15 additions & 7 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\CatalogImportExport\Model\Import\Product\MediaGalleryProcessor;
use Magento\CatalogImportExport\Model\Import\Product\ImageTypeProcessor;
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface as ValidatorInterface;
use Magento\CatalogImportExport\Model\StockItemImporterInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
Expand Down Expand Up @@ -703,6 +704,13 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
*/
private $catalogConfig;

/**
* Stock Item Importer
*
* @var StockItemImporterInterface $stockItemImporter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently Magento does not use any standard which requires adding variable name in this kind of docblock

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

*/
private $stockItemImporter;

/**
* @var ImageTypeProcessor
*/
Expand Down Expand Up @@ -754,6 +762,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
* @param array $data
* @param array $dateAttrCodes
* @param CatalogConfig $catalogConfig
* @param StockItemImporterInterface $stockItemImporter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Order of parameters in the docblock is different from the method signature

Copy link
Contributor

@RomaKis RomaKis Mar 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

* @param ImageTypeProcessor $imageTypeProcessor
* @param MediaGalleryProcessor $mediaProcessor
* @throws \Magento\Framework\Exception\LocalizedException
Expand Down Expand Up @@ -801,7 +810,8 @@ public function __construct(
array $dateAttrCodes = [],
CatalogConfig $catalogConfig = null,
ImageTypeProcessor $imageTypeProcessor = null,
MediaGalleryProcessor $mediaProcessor = null
MediaGalleryProcessor $mediaProcessor = null,
StockItemImporterInterface $stockItemImporter = null
) {
$this->_eventManager = $eventManager;
$this->stockRegistry = $stockRegistry;
Expand Down Expand Up @@ -835,7 +845,8 @@ public function __construct(
$this->catalogConfig = $catalogConfig ?: ObjectManager::getInstance()->get(CatalogConfig::class);
$this->imageTypeProcessor = $imageTypeProcessor ?: ObjectManager::getInstance()->get(ImageTypeProcessor::class);
$this->mediaProcessor = $mediaProcessor ?: ObjectManager::getInstance()->get(MediaGalleryProcessor::class);

$this->stockItemImporter = $stockItemImporter ?: ObjectManager::getInstance()
->get(StockItemImporterInterface::class);
parent::__construct(
$jsonHelper,
$importExportData,
Expand All @@ -851,7 +862,6 @@ public function __construct(
) ? $data['option_entity'] : $optionFactory->create(
['data' => ['product_entity' => $this]]
);

$this->_initAttributeSets()
->_initTypeModels()
->_initSkus()
Expand Down Expand Up @@ -2128,9 +2138,6 @@ protected function _saveProductWebsites(array $websiteData)
*/
protected function _saveStockItem()
{
/** @var $stockResource \Magento\CatalogInventory\Model\ResourceModel\Stock\Item */
$stockResource = $this->_stockResItemFac->create();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it was the only usage of _stockResItemFac property in the class.
In this case it may be deprecated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

$entityTable = $stockResource->getMainTable();
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$stockData = [];
$productIdsToReindex = [];
Expand Down Expand Up @@ -2158,6 +2165,7 @@ protected function _saveStockItem()
array_intersect_key($rowData, $this->defaultStockData),
$row
);
$row['sku'] = $sku;

if ($this->stockConfiguration->isQty(
$this->skuProcessor->getNewSku($sku)['type_id']
Expand Down Expand Up @@ -2185,7 +2193,7 @@ protected function _saveStockItem()

// Insert rows
if (!empty($stockData)) {
$this->_connection->insertOnDuplicate($entityTable, array_values($stockData));
$this->stockItemImporter->import($stockData);
}

$this->reindexProducts($productIdsToReindex);
Expand Down
69 changes: 69 additions & 0 deletions app/code/Magento/CatalogImportExport/Model/StockItemImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogImportExport\Model;

use Magento\CatalogInventory\Model\ResourceModel\Stock\Item;
use Magento\CatalogInventory\Model\ResourceModel\Stock\ItemFactory;
use Magento\Framework\Exception\CouldNotSaveException;
use Psr\Log\LoggerInterface;

class StockItemImporter implements StockItemImporterInterface
{
/**
* Stock Item Resource Factory
*
* @var ItemFactory $stockResourceItemFactory
*/
private $stockResourceItemFactory;

/**
* @var LoggerInterface
*/
private $logger;

/**
* StockItemImporter constructor
*
* @param ItemFactory $stockResourceItemFactory
* @param LoggerInterface $logger
*/
public function __construct(
ItemFactory $stockResourceItemFactory,
LoggerInterface $logger
) {
$this->stockResourceItemFactory = $stockResourceItemFactory;
$this->logger = $logger;
}

/**
* Handle Import of Stock Item Data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like @inheritdoc may be used here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

*
* @param array $stockData
* @return void
* @throws CouldNotSaveException
*/
public function import(array $stockData)
{
/** @var $stockItemResource Item */
$stockItemResource = $this->stockResourceItemFactory->create();
$entityTable = $stockItemResource->getMainTable();
try {
$stockImportData = array_map(
function ($stockItemData) {
unset($stockItemData['sku']);
return $stockItemData;
},
array_values($stockData)
);
$stockItemResource->getConnection()->insertOnDuplicate($entityTable, $stockImportData);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
throw new CouldNotSaveException(__('Invalid Stock data for insert'), $e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogImportExport\Model;

/**
* Interface StockItemImporterInterface
*
* @api
*/
interface StockItemImporterInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no default implementation of this interface in CE, just in MSI

that will lead to Fatal Error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

{
/**
* Handle Import of Stock Item Data
*
* @param array $stockData
* @return void
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Validation\ValidationException
*/
public function import(array $stockData);
}
1 change: 1 addition & 0 deletions app/code/Magento/CatalogImportExport/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\CatalogImportExport\Model\Export\RowCustomizerInterface" type="Magento\CatalogImportExport\Model\Export\RowCustomizer\Composite" />
<preference for="Magento\CatalogImportExport\Model\StockItemImporterInterface" type="Magento\CatalogImportExport\Model\StockItemImporter" />
<type name="Magento\ImportExport\Model\Import">
<plugin name="catalogProductFlatIndexerImport" type="Magento\CatalogImportExport\Model\Indexer\Product\Flat\Plugin\Import" />
<plugin name="invalidatePriceIndexerOnImport" type="Magento\CatalogImportExport\Model\Indexer\Product\Price\Plugin\Import" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogInventory\Api;

use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\Framework\Exception\LocalizedException;

/**
* @api
*/
interface RegisterProductSaleInterface
{
/**
* Subtract product qtys from stock
* Return array of items that require full save
*
* Method signature is unchanged for backward compatibility
*
* @param float[] $items
* @param int $websiteId
* @return StockItemInterface[]
* @throws LocalizedException
*/
public function registerProductsSale($items, $websiteId = null);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogInventory\Api;

/**
* @api
*/
interface RevertProductSaleInterface
{
/**
* Revert register product sale
*
* Method signature is unchanged for backward compatibility
*
* @param string[] $items
* @param int $websiteId
* @return bool
*/
public function revertProductsSale($items, $websiteId = null);
}
6 changes: 4 additions & 2 deletions app/code/Magento/CatalogInventory/Model/StockManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Magento\CatalogInventory\Model;

use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Api\RegisterProductSaleInterface;
use Magento\CatalogInventory\Api\RevertProductSaleInterface;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Api\StockManagementInterface;
use Magento\CatalogInventory\Model\ResourceModel\QtyCounterInterface;
Expand All @@ -14,9 +16,9 @@
use Magento\CatalogInventory\Model\ResourceModel\Stock as ResourceStock;

/**
* Class StockManagement
* Implements a few interfaces for backward compatibility
*/
class StockManagement implements StockManagementInterface
class StockManagement implements StockManagementInterface, RegisterProductSaleInterface, RevertProductSaleInterface
{
/**
* @var StockRegistryProviderInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ItemsForReindex
/**
* @var array
*/
protected $itemsForReindex;
protected $itemsForReindex = [];

/**
* @param array $items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ public function execute(EventObserver $observer)
$items,
$quote->getStore()->getWebsiteId()
);
$this->itemsForReindex->setItems($itemsForReindex);

if (count($itemsForReindex)) {
$this->itemsForReindex->setItems($itemsForReindex);
}
$quote->setInventoryProcessed(true);
return $this;
}
Expand Down
Loading