forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request magento#994 from magento-engcom/MSI-666_replicate_…
…source_items MSI-666: replicate source items
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
app/code/Magento/InventoryCatalogAdminUi/Plugin/Catalog/CopySourceItemsPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryCatalogAdminUi\Plugin\Catalog; | ||
|
||
use Magento\Catalog\Model\Product; | ||
use Magento\Catalog\Model\Product\Copier; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\Api\SearchCriteriaBuilderFactory; | ||
use Magento\InventoryApi\Api\Data\SourceItemInterface; | ||
use Magento\InventoryApi\Api\SourceItemRepositoryInterface; | ||
use Magento\InventoryCatalogAdminUi\Observer\SourceItemsProcessor; | ||
|
||
/** | ||
* Copies source items from the original product to the duplicate | ||
*/ | ||
class CopySourceItemsPlugin | ||
{ | ||
/** | ||
* @var SourceItemRepositoryInterface | ||
*/ | ||
private $sourceItemRepository; | ||
|
||
/** | ||
* @var SearchCriteriaBuilderFactory | ||
*/ | ||
private $searchCriteriaBuilderFactory; | ||
|
||
/** | ||
* @var SourceItemsProcessor | ||
*/ | ||
private $sourceItemsProcessor; | ||
|
||
/** | ||
* @param SourceItemRepositoryInterface $sourceItemRepository | ||
* @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory | ||
* @param SourceItemsProcessor $sourceItemsProcessor | ||
*/ | ||
public function __construct( | ||
SourceItemRepositoryInterface $sourceItemRepository, | ||
SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory, | ||
SourceItemsProcessor $sourceItemsProcessor | ||
) { | ||
$this->sourceItemRepository = $sourceItemRepository; | ||
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory; | ||
$this->sourceItemsProcessor = $sourceItemsProcessor; | ||
} | ||
|
||
/** | ||
* @param Copier $subject | ||
* @param Product $result | ||
* @param Product $product | ||
* @return Product $result | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterCopy( | ||
Copier $subject, | ||
Product $result, | ||
Product $product | ||
) { | ||
$this->copySourceItems($product->getSku(), $result->getSku()); | ||
return $result; | ||
} | ||
|
||
/** | ||
* @param string $sku | ||
* @return array | ||
*/ | ||
private function getSourceItems(string $sku): array | ||
{ | ||
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */ | ||
$searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create(); | ||
$searchCriteria = $searchCriteriaBuilder | ||
->addFilter(SourceItemInterface::SKU, $sku) | ||
->create(); | ||
return $this->sourceItemRepository->getList($searchCriteria)->getItems(); | ||
} | ||
|
||
/** | ||
* @param string $originalSku | ||
* @param string $duplicateSku | ||
*/ | ||
private function copySourceItems(string $originalSku, string $duplicateSku) | ||
{ | ||
$sourceItems = $this->getSourceItems($originalSku); | ||
|
||
$duplicateItemData = []; | ||
if ($sourceItems) { | ||
foreach ($sourceItems as $sourceItem) { | ||
$duplicateItemData[] = [ | ||
SourceItemInterface::SKU => $duplicateSku, | ||
SourceItemInterface::SOURCE_CODE => $sourceItem->getSourceCode(), | ||
SourceItemInterface::QUANTITY => $sourceItem->getQuantity(), | ||
SourceItemInterface::STATUS => $sourceItem->getStatus() | ||
]; | ||
} | ||
} | ||
|
||
$this->sourceItemsProcessor->process( | ||
$duplicateSku, | ||
$duplicateItemData | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters