Created
September 15, 2016 06:07
-
-
Save mrkhoa99/bd43fe34fc51d1f7fd38fbf7d13d384b to your computer and use it in GitHub Desktop.
Revisions
-
mrkhoa99 created this gist
Sep 15, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ $obj = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Catalog\Model\Product $product */ $productObject = $obj->get('Magento\Catalog\Model\Product'); $product = $productObject->loadByAttribute('sku', 'Test Test'); $linkDataAll = []; $skuLinks = "0012365,test1233,789456"; $skuLinks = explode(",",$skuLinks); foreach($skuLinks as $skuLink) { //check first that the product exist $linkedProduct = $productObject->loadByAttribute("sku",$skuLink); if($linkedProduct) { /** @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("related"); $linkDataAll[] = $linkData; } } if($linkDataAll) { print(count($linkDataAll)); //gives 3 $product->setProductLinks($linkDataAll); } // $product->save();