Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BulkConfigurationTransfer.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
19 {
23  private $resourceConnection;
24 
28  private $getProductTypesBySkus;
29 
33  private $isSourceItemManagementAllowedForProductType;
34 
41  public function __construct(
42  ResourceConnection $resourceConnection,
43  GetProductTypesBySkusInterface $getProductTypesBySkus,
44  IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
45  ) {
46  $this->resourceConnection = $resourceConnection;
47  $this->getProductTypesBySkus = $getProductTypesBySkus;
48  $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
49  }
50 
58  public function execute(
59  array $skus,
60  string $originSource,
61  string $destinationSource
62  ) {
63  $tableName = $this->resourceConnection->getTableName('inventory_low_stock_notification_configuration');
64  $connection = $this->resourceConnection->getConnection();
65 
66  $types = $this->getProductTypesBySkus->execute($skus);
67 
68  foreach ($types as $sku => $type) {
69  if ($this->isSourceItemManagementAllowedForProductType->execute($type)) {
70  foreach ($skus as $sku) {
71  $qry = $connection
72  ->select()
73  ->from($tableName, 'notify_stock_qty')
74  ->where('sku = ?', $sku)
75  ->where('source_code = ?', $originSource);
76 
77  $res = $connection->fetchOne($qry);
78 
79  $notifyStockQty = ($res === null || $res === false) ? null : (float) $res;
80  try {
81  $connection->insert(
82  $tableName,
83  [
84  'source_code' => $destinationSource,
85  'sku' => $sku,
86  'notify_stock_qty' => $notifyStockQty,
87  ]
88  );
89  } catch (DuplicateException $e) {
90  // Do not overwrite an existing configuration if the item was not assigned to the source
91  if ($res !== false) {
92  $connection->update(
93  $tableName,
94  ['notify_stock_qty' => $notifyStockQty],
95  $connection->quoteInto('sku = ?', $sku) . ' AND ' .
96  $connection->quoteInto('source_code = ?', $destinationSource)
97  );
98  }
99  }
100  }
101  }
102  }
103  }
104 }
$tableName
Definition: trigger.php:13
foreach($websiteCodes as $websiteCode) $skus
$type
Definition: item.phtml:13
__construct(ResourceConnection $resourceConnection, GetProductTypesBySkusInterface $getProductTypesBySkus, IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType)
$connection
Definition: bulk.php:13