9 use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory;
10 use Magento\Catalog\Api\Data\ProductLinkExtensionFactory;
106 ->get(\
Magento\Catalog\Api\Data\ProductLinkInterfaceFactory::class);
108 ->get(\
Magento\Catalog\Api\Data\ProductLinkExtensionFactory::class);
119 $extensions = $this->dataObjectProcessor->buildOutputDataArray(
120 $entity->getExtensionAttributes(),
121 \Magento\Catalog\Api\Data\ProductLinkExtensionInterface::class
123 $extensions = is_array($extensions) ? $extensions : [];
128 unset(
$data[
'extension_attributes']);
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()],
138 $linkTypesToId[
$entity->getLinkType()]
140 }
catch (\Exception $exception) {
155 $linkTypes = $this->getLinkTypeProvider()->getLinkTypes();
156 foreach (array_keys($linkTypes) as $linkTypeName) {
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();
176 if (method_exists($productLinkExtension, $setterName)) {
180 $productLink->setExtensionAttributes($productLinkExtension);
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()],
200 $linkTypesToId[
$entity->getLinkType()]
206 'Product with SKU \'%1\' is not linked to product with SKU \'%2\'',
207 $entity->getLinkedProductSku(),
214 $this->getLinkResource()->deleteProductLink($linkId);
215 }
catch (\Exception $exception) {
226 $linkItems = $this->linkManagement->getLinkedItemsByType($sku,
$type);
228 foreach ($linkItems as $linkItem) {
229 if ($linkItem->getLinkedProductSku() == $linkedProductSku) {
230 return $this->
delete($linkItem);
233 throw new NoSuchEntityException(
235 'Product %1 doesn\'t have linked %2 as %3',
248 private function getLinkResource()
250 if (
null === $this->linkResource) {
260 private function getLinkTypeProvider()
262 if (
null === $this->linkTypeProvider) {
264 ->get(\
Magento\Catalog\Model\Product\LinkTypeProvider::class);
272 private function getMetadataPool()
274 if (
null === $this->metadataPool) {
276 ->get(\
Magento\Framework\EntityManager\MetadataPool::class);
static snakeCaseToUpperCamelCase($input)
call_user_func($callable, $param)
$entityCollectionProvider
__construct(\Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Catalog\Model\ProductLink\CollectionProvider $entityCollectionProvider, \Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks $linkInitializer, \Magento\Catalog\Model\ProductLink\Management $linkManagement, \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor, \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory=null, \Magento\Catalog\Api\Data\ProductLinkExtensionFactory $productLinkExtensionFactory=null)
$productLinkExtensionFactory
save(\Magento\Catalog\Api\Data\ProductLinkInterface $entity)
getList(\Magento\Catalog\Api\Data\ProductInterface $product)
deleteById($sku, $type, $linkedProductSku)
if(!isset($_GET['name'])) $name