Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProcessSourceItemsAfterSaveAssociatedLinks.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 
21 {
25  private $getSourceItemsBySku;
26 
30  private $sourceItemsProcessor;
31 
36  public function __construct(
37  GetSourceItemsBySkuInterface $getSourceItemsBySku,
38  SourceItemsProcessor $sourceItemsProcessor
39  ) {
40  $this->getSourceItemsBySku = $getSourceItemsBySku;
41  $this->sourceItemsProcessor = $sourceItemsProcessor;
42  }
43 
51  public function afterSaveProductRelations(
52  Link $subject,
53  Link $result,
55  ): Link {
56  if ($product->getTypeId() !== GroupedProductType::TYPE_CODE) {
57  return $result;
58  }
59 
60  foreach ($product->getProductLinks() as $productLink) {
61  if ($productLink->getLinkType() === 'associated') {
62  $this->processSourceItemsForSku($productLink->getLinkedProductSku());
63  }
64  }
65 
66  return $result;
67  }
68 
75  private function processSourceItemsForSku(string $sku): void
76  {
77  $processData = [];
78 
79  foreach ($this->getSourceItemsBySku->execute($sku) as $sourceItem) {
80  $processData[] = [
84  ];
85  }
86 
87  if (!empty($processData)) {
88  $this->sourceItemsProcessor->process($sku, $processData);
89  }
90  }
91 }
__construct(GetSourceItemsBySkuInterface $getSourceItemsBySku, SourceItemsProcessor $sourceItemsProcessor)