Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Repository.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory;
10 use Magento\Catalog\Api\Data\ProductLinkExtensionFactory;
18 
23 {
27  protected $metadataPool;
28 
33 
37  protected $linkResource;
38 
42  protected $linkTypeProvider;
43 
47  protected $productRepository;
48 
53 
57  protected $linkInitializer;
58 
62  protected $linkManagement;
63 
68 
73 
78 
91  public function __construct(
93  \Magento\Catalog\Model\ProductLink\CollectionProvider $entityCollectionProvider,
94  \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $linkInitializer,
95  \Magento\Catalog\Model\ProductLink\Management $linkManagement,
96  \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
97  \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory = null,
98  \Magento\Catalog\Api\Data\ProductLinkExtensionFactory $productLinkExtensionFactory = null
99  ) {
100  $this->productRepository = $productRepository;
101  $this->entityCollectionProvider = $entityCollectionProvider;
102  $this->linkInitializer = $linkInitializer;
103  $this->linkManagement = $linkManagement;
104  $this->dataObjectProcessor = $dataObjectProcessor;
105  $this->productLinkFactory = $productLinkFactory ?: ObjectManager::getInstance()
106  ->get(\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory::class);
107  $this->productLinkExtensionFactory = $productLinkExtensionFactory ?: ObjectManager::getInstance()
108  ->get(\Magento\Catalog\Api\Data\ProductLinkExtensionFactory::class);
109  }
110 
114  public function save(\Magento\Catalog\Api\Data\ProductLinkInterface $entity)
115  {
116  $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku());
117  $product = $this->productRepository->get($entity->getSku());
118  $links = [];
119  $extensions = $this->dataObjectProcessor->buildOutputDataArray(
120  $entity->getExtensionAttributes(),
121  \Magento\Catalog\Api\Data\ProductLinkExtensionInterface::class
122  );
123  $extensions = is_array($extensions) ? $extensions : [];
124  $data = $entity->__toArray();
125  foreach ($extensions as $attributeCode => $attribute) {
127  }
128  unset($data['extension_attributes']);
129  $data['product_id'] = $linkedProduct->getId();
130  $links[$linkedProduct->getId()] = $data;
131 
132  try {
133  $linkTypesToId = $this->getLinkTypeProvider()->getLinkTypes();
134  $productData = $this->getMetadataPool()->getHydrator(ProductInterface::class)->extract($product);
135  $this->getLinkResource()->saveProductLinks(
136  $productData[$this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()],
137  $links,
138  $linkTypesToId[$entity->getLinkType()]
139  );
140  } catch (\Exception $exception) {
141  throw new CouldNotSaveException(__('The linked products data is invalid. Verify the data and try again.'));
142  }
143  return true;
144  }
145 
152  public function getList(\Magento\Catalog\Api\Data\ProductInterface $product)
153  {
154  $output = [];
155  $linkTypes = $this->getLinkTypeProvider()->getLinkTypes();
156  foreach (array_keys($linkTypes) as $linkTypeName) {
157  $collection = $this->entityCollectionProvider->getCollection($product, $linkTypeName);
158  foreach ($collection as $item) {
160  $productLink = $this->productLinkFactory->create();
161  $productLink->setSku($product->getSku())
162  ->setLinkType($linkTypeName)
163  ->setLinkedProductSku($item['sku'])
164  ->setLinkedProductType($item['type'])
165  ->setPosition($item['position']);
166  if (isset($item['custom_attributes'])) {
167  $productLinkExtension = $productLink->getExtensionAttributes();
168  if ($productLinkExtension === null) {
169  $productLinkExtension = $this->productLinkExtensionFactory()->create();
170  }
171  foreach ($item['custom_attributes'] as $option) {
172  $name = $option['attribute_code'];
173  $value = $option['value'];
175  // Check if setter exists
176  if (method_exists($productLinkExtension, $setterName)) {
177  call_user_func([$productLinkExtension, $setterName], $value);
178  }
179  }
180  $productLink->setExtensionAttributes($productLinkExtension);
181  }
182  $output[] = $productLink;
183  }
184  }
185  return $output;
186  }
187 
192  {
193  $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku());
194  $product = $this->productRepository->get($entity->getSku());
195  $linkTypesToId = $this->getLinkTypeProvider()->getLinkTypes();
196  $productData = $this->getMetadataPool()->getHydrator(ProductInterface::class)->extract($product);
197  $linkId = $this->getLinkResource()->getProductLinkId(
198  $productData[$this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()],
199  $linkedProduct->getId(),
200  $linkTypesToId[$entity->getLinkType()]
201  );
202 
203  if (!$linkId) {
204  throw new NoSuchEntityException(
205  __(
206  'Product with SKU \'%1\' is not linked to product with SKU \'%2\'',
207  $entity->getLinkedProductSku(),
208  $entity->getSku()
209  )
210  );
211  }
212 
213  try {
214  $this->getLinkResource()->deleteProductLink($linkId);
215  } catch (\Exception $exception) {
216  throw new CouldNotSaveException(__('The linked products data is invalid. Verify the data and try again.'));
217  }
218  return true;
219  }
220 
224  public function deleteById($sku, $type, $linkedProductSku)
225  {
226  $linkItems = $this->linkManagement->getLinkedItemsByType($sku, $type);
228  foreach ($linkItems as $linkItem) {
229  if ($linkItem->getLinkedProductSku() == $linkedProductSku) {
230  return $this->delete($linkItem);
231  }
232  }
233  throw new NoSuchEntityException(
234  __(
235  'Product %1 doesn\'t have linked %2 as %3',
236  [
237  $sku,
238  $linkedProductSku,
239  $type
240  ]
241  )
242  );
243  }
244 
248  private function getLinkResource()
249  {
250  if (null === $this->linkResource) {
252  ->get(\Magento\Catalog\Model\ResourceModel\Product\Link::class);
253  }
254  return $this->linkResource;
255  }
256 
260  private function getLinkTypeProvider()
261  {
262  if (null === $this->linkTypeProvider) {
263  $this->linkTypeProvider = \Magento\Framework\App\ObjectManager::getInstance()
264  ->get(\Magento\Catalog\Model\Product\LinkTypeProvider::class);
265  }
267  }
268 
272  private function getMetadataPool()
273  {
274  if (null === $this->metadataPool) {
276  ->get(\Magento\Framework\EntityManager\MetadataPool::class);
277  }
278  return $this->metadataPool;
279  }
280 }
__()
Definition: __.php:13
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
$entity
Definition: element.phtml:22
$productData
$linkedProduct
if(!isset($_GET['name'])) $name
Definition: log.php:14