Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BulkSourceAssign.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 
24 {
28  private $resourceConnection;
29 
33  private $isSourceItemManagementAllowedForProductType;
34 
38  private $getProductTypesBySkus;
39 
46  public function __construct(
47  ResourceConnection $resourceConnection,
48  GetProductTypesBySkusInterface $getProductTypesBySkus,
49  IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
50  ) {
51  $this->resourceConnection = $resourceConnection;
52  $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
53  $this->getProductTypesBySkus = $getProductTypesBySkus;
54  }
55 
62  public function execute(array $skus, array $sourceCodes): int
63  {
64  $types = $this->getProductTypesBySkus->execute($skus);
65 
66  $connection = $this->resourceConnection->getConnection();
67  $tableName = $this->resourceConnection->getTableName(SourceItem::TABLE_NAME_SOURCE_ITEM);
68 
69  $count = 0;
70  foreach ($types as $sku => $type) {
71  if ($this->isSourceItemManagementAllowedForProductType->execute($type)) {
72  foreach ($sourceCodes as $sourceCode) {
73  try {
74  $connection->insert($tableName, [
79  ]);
80  $count++;
81  } catch (DuplicateException $e) {
82  // Skip if source assignment is duplicated
83  continue;
84  }
85  }
86  }
87  }
88 
89  return $count;
90  }
91 }
$tableName
Definition: trigger.php:13
$count
Definition: recent.phtml:13
foreach($websiteCodes as $websiteCode) $skus
$type
Definition: item.phtml:13
$sourceCode
Definition: inventory.phtml:11
__construct(ResourceConnection $resourceConnection, GetProductTypesBySkusInterface $getProductTypesBySkus, IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType)
$connection
Definition: bulk.php:13