Skip to content

Commit

Permalink
Merge pull request magento#994 from magento-engcom/MSI-666_replicate_…
Browse files Browse the repository at this point in the history
…source_items

MSI-666: replicate source items
  • Loading branch information
maghamed authored Apr 22, 2018
2 parents 91e4710 + 435291b commit f2fdae5
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
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
);
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/InventoryCatalogAdminUi/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@
</argument>
</arguments>
</virtualType>
<type name="Magento\Catalog\Model\Product\Copier">
<plugin name="copy_source_items" type="Magento\InventoryCatalogAdminUi\Plugin\Catalog\CopySourceItemsPlugin"/>
</type>
</config>

0 comments on commit f2fdae5

Please sign in to comment.