0

I am using below script to assign related sku to main sku. Script works well but the only issue is it removes old sku & assign new set of sku.

I want to know if their is any way we can just assign new sku & keep existing one's ?

$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();
4
  • any thoughts on this ?
    – devhs
    May 20, 2021 at 7:49
  • Get old SKU first save it in variable and then get a new one and then merge them and set it to setLinkedProductSku using $skuLink May 20, 2021 at 7:52
  • Thanks for the suggestion @NileshDubey, but it will still have to save multiple sku (old + new)
    – devhs
    May 20, 2021 at 8:02
  • any thoughts on this ?
    – devhs
    May 20, 2021 at 9:07

2 Answers 2

1

In your script , you have to get product's related products and store into one variable. Then you have to merge array and find unique from both the array.

Please check your updated script.

$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;
    }

}

$existingRelatedProducts = $product->getProductLinks();

if($existingRelatedProducts) {
   $linkDataAll = array_unique(array_merge($linkDataAll,$existingRelatedProducts));
}

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

You can save the "old" array:

$oldData = $product->getProductLinks();

and merge it with the new array, before saving the product:

if($linkDataAll) {
   print(count($linkDataAll)); //gives 3
   $linkDataAll = array_merge($linkDataAll,$oldData);
   print(count($linkDataAll)); //should give all
   $product->setProductLinks($linkDataAll);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.