SetDataToLegacyStockItem.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryCatalog\Model\ResourceModel;
  8. use Magento\CatalogInventory\Api\Data\StockItemInterface;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface;
  11. /**
  12. * Set data to legacy cataloginventory_stock_item table via plain MySql query
  13. */
  14. class SetDataToLegacyStockItem
  15. {
  16. /**
  17. * @var ResourceConnection
  18. */
  19. private $resourceConnection;
  20. /**
  21. * @var GetProductIdsBySkusInterface
  22. */
  23. private $getProductIdsBySkus;
  24. /**
  25. * @param ResourceConnection $resourceConnection
  26. * @param GetProductIdsBySkusInterface $getProductIdsBySkus
  27. */
  28. public function __construct(
  29. ResourceConnection $resourceConnection,
  30. GetProductIdsBySkusInterface $getProductIdsBySkus
  31. ) {
  32. $this->resourceConnection = $resourceConnection;
  33. $this->getProductIdsBySkus = $getProductIdsBySkus;
  34. }
  35. /**
  36. * @param string $sku
  37. * @param float $quantity
  38. * @param int $status
  39. * @return void
  40. */
  41. public function execute(string $sku, float $quantity, int $status)
  42. {
  43. $productIds = $this->getProductIdsBySkus->execute([$sku]);
  44. if (isset($productIds[$sku])) {
  45. $productId = $productIds[$sku];
  46. $connection = $this->resourceConnection->getConnection();
  47. $connection->update(
  48. $this->resourceConnection->getTableName('cataloginventory_stock_item'),
  49. [
  50. StockItemInterface::QTY => $quantity,
  51. StockItemInterface::IS_IN_STOCK => $status,
  52. ],
  53. [
  54. StockItemInterface::PRODUCT_ID . ' = ?' => $productId,
  55. 'website_id = ?' => 0,
  56. ]
  57. );
  58. }
  59. }
  60. }