Skip to content

Commit

Permalink
Adapt current logic of Product Qty Update on the Product Form in admi…
Browse files Browse the repository at this point in the history
…n panel to save data in default Source magento#169
  • Loading branch information
VitaliyBoyko committed Nov 25, 2017
1 parent 5e692c8 commit e676db3
Showing 1 changed file with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Observer;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Controller\Adminhtml\Product\Save;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
use Magento\InventoryCatalog\Api\DefaultSourceProviderInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;

/**
* Save source product relations during product persistence via controller
Expand All @@ -21,14 +27,36 @@ class ProcessSourceItemsObserver implements ObserverInterface
/**
* @var SourceItemsProcessor
*/
private $sourceItemsProcessor;
protected $sourceItemsProcessor;
/**
* @var DefaultSourceProviderInterface
*/
protected $defaultSourceProvider;
/**
* @var SourceItemInterfaceFactory
*/
protected $sourceItemInterfaceFactory;
/**
* @var SourceItemsSaveInterface
*/
protected $sourceItemsSave;

/**
* @param SourceItemsProcessor $sourceItemsProcessor
* @param DefaultSourceProviderInterface $defaultSourceProvider
* @param SourceItemInterfaceFactory $sourceItemInterfaceFactory
* @param SourceItemsSaveInterface $sourceItemsSave
*/
public function __construct(SourceItemsProcessor $sourceItemsProcessor)
{
public function __construct(
SourceItemsProcessor $sourceItemsProcessor,
DefaultSourceProviderInterface $defaultSourceProvider,
SourceItemInterfaceFactory $sourceItemInterfaceFactory,
SourceItemsSaveInterface $sourceItemsSave
) {
$this->sourceItemsProcessor = $sourceItemsProcessor;
$this->defaultSourceProvider = $defaultSourceProvider;
$this->sourceItemInterfaceFactory = $sourceItemInterfaceFactory;
$this->sourceItemsSave = $sourceItemsSave;
}

/**
Expand All @@ -53,5 +81,29 @@ public function execute(EventObserver $observer)
$product->getSku(),
$assignedSources
);

$this->updateDefaultSourceQty($controller);
}

/**
* @param $controller Save
*/
public function updateDefaultSourceQty($controller)
{
/** @var $sourceItem SourceItemInterface */
$sourceItem = $this->sourceItemInterfaceFactory->create();
$productParams = $controller->getRequest()->getParam('product');

$sku = $productParams['sku'];
$sourceItem->setSku($sku);
$qtyAndStockStatus = $productParams['quantity_and_stock_status'];
$qty = $qtyAndStockStatus['qty'];
$sourceItem->setQuantity($qty);
$stockStatus = $qtyAndStockStatus['is_in_stock'];
$sourceItem->setStatus($stockStatus);
$defaultSourceId = $this->defaultSourceProvider->getId();
$sourceItem->setSourceId($defaultSourceId);

$this->sourceItemsSave->execute([$sourceItem]);
}
}

0 comments on commit e676db3

Please sign in to comment.