Skip to content

Instantly share code, notes, and snippets.

@mrkhoa99
Created September 15, 2016 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mrkhoa99/bd43fe34fc51d1f7fd38fbf7d13d384b to your computer and use it in GitHub Desktop.
Save mrkhoa99/bd43fe34fc51d1f7fd38fbf7d13d384b to your computer and use it in GitHub Desktop.
Set related products programatically Magento 2
@harrywutech
Copy link

Hi there, do you know where can i put and run this code? Thank you

@sfblaauw
Copy link

Nice, thanks

@samanaveen
Copy link

catalog_product_link_attribute_int table is not updating using above code. Ii fixed that issue. So please use below code....
$linkDataAll = [];
$skuLinks = "0012365,test1233,789456";
$skuLinks = explode(",",$skuLinks);

            foreach($skuLinks as $n => $skuLink) {
                //check first that the product exist

                try {
					echo '<br /> linked  = '.$skuLink;
                    $linkedProduct = $productRepository->get($skuLink);
                } catch (\Magento\Framework\Exception\NoSuchEntityException $e){
                    echo '<br />'.$skuLink.'   '.$e->getMessage();
                    \Magento\Framework\App\ObjectManager::getInstance()->create(\Psr\Log\LoggerInterface::class)->info('sku not exist : '.$sku);

                    $linkedProduct = false;
                }

                if($linkedProduct && $skuLink != $product->getSku()) {
                    /** @var  \Magento\Catalog\Api\Data\ProductLinkInterface $productLinks */
                    $productLinks = $obj->create('Magento\Catalog\Api\Data\ProductLinkInterface');
                    $linkData = $productLinks //Magento\Catalog\Api\Data\ProductLinkInterface
                        ->setSku($product->getSku())
                        ->setLinkedProductSku($skuLink)
                        //->setLinkType("matchingfaucets");
                        ->setLinkType($type)
                        ->setPosition($n + 1);
                    $linkDataAll[] = $linkData;
                }

            }
            if($linkDataAll) {
                print(count($linkDataAll)); //gives 3
                $product->setProductLinks($linkDataAll);
            }
            $product->save();

@Anxiro
Copy link

Anxiro commented Sep 19, 2018

Using $product->save(); is deprecated and should be avoided. You should use the ProductRepositoryInterface to save the product.

And ofcourse, using the ObjectManager is bad behavior and should not be used on production environments.

@Aman4111
Copy link

Aman4111 commented Jun 15, 2021

Hi,
This code is working for me but when I added the related products with this code. Other up-sell and cross-sell removed issues. When I added the related, cross-sell, and up-sell together then only the last value inserted in the product.
Can you please guide me?

@anhnhan1997
Copy link

Hi,
This code is working for me but when I added the related products with this code. Other up-sell and cross-sell removed issues. When I added the related, cross-sell, and up-sell together then only the last value inserted in the product.
Can you please guide me?

pls use class Magento\Catalog\Api\Data\ProductLinkInterfaceFactory instead of Magento\Catalog\Api\Data\ProductLinkInterface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment