Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SaveStockItemConfiguration.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 
21 {
25  private $resourceConnection;
26 
30  private $getProductIdsBySkus;
31 
35  private $defaultStockProvider;
36 
42  public function __construct(
43  ResourceConnection $resourceConnection,
44  GetProductIdsBySkusInterface $getProductIdsBySkus,
45  DefaultStockProviderInterface $defaultStockProvider
46  ) {
47  $this->resourceConnection = $resourceConnection;
48  $this->getProductIdsBySkus = $getProductIdsBySkus;
49  $this->defaultStockProvider = $defaultStockProvider;
50  }
51 
57  public function execute(string $sku, int $stockId, StockItemConfigurationInterface $stockItemConfiguration): void
58  {
59  $productId = $this->getProductIdsBySkus->execute([$sku])[$sku];
60 
61  // TODO We ignore $stockId and use $legacyStockId until we have proper multi-stock item configuration
62  $legacyStockId = $this->defaultStockProvider->getId();
63 
64  $connection = $this->resourceConnection->getConnection();
65  $connection->update(
66  $this->resourceConnection->getTableName('cataloginventory_stock_item'),
67  $this->getBinds($stockItemConfiguration),
68  [
70  StockItemInterface::STOCK_ID . ' = ?' => $legacyStockId,
71  'website_id = ?' => 0,
72  ]
73  );
74  }
75 
81  private function getBinds(StockItemConfigurationInterface $stockItemConfiguration): array
82  {
83  return [
84  StockItemInterface::IS_QTY_DECIMAL => $stockItemConfiguration->isQtyDecimal(),
85  StockItemInterface::USE_CONFIG_MIN_QTY => $stockItemConfiguration->isUseConfigMinQty(),
86  StockItemInterface::MIN_QTY => $stockItemConfiguration->getMinQty(),
88  StockItemInterface::MIN_SALE_QTY => $stockItemConfiguration->getMinSaleQty(),
90  StockItemInterface::MAX_SALE_QTY => $stockItemConfiguration->getMaxSaleQty(),
92  StockItemInterface::BACKORDERS => $stockItemConfiguration->getBackorders(),
94  StockItemInterface::NOTIFY_STOCK_QTY => $stockItemConfiguration->getNotifyStockQty(),
96  StockItemInterface::QTY_INCREMENTS => $stockItemConfiguration->getQtyIncrements(),
100  StockItemInterface::MANAGE_STOCK => $stockItemConfiguration->isManageStock(),
101  StockItemInterface::LOW_STOCK_DATE => $stockItemConfiguration->getLowStockDate(),
102  StockItemInterface::IS_DECIMAL_DIVIDED => $stockItemConfiguration->isDecimalDivided(),
104  StockItemInterface::IS_IN_STOCK => $stockItemConfiguration->getExtensionAttributes()->getIsInStock()
105  ];
106  }
107 }
execute(string $sku, int $stockId, StockItemConfigurationInterface $stockItemConfiguration)
__construct(ResourceConnection $resourceConnection, GetProductIdsBySkusInterface $getProductIdsBySkus, DefaultStockProviderInterface $defaultStockProvider)
$connection
Definition: bulk.php:13