-
-
Save mrkhoa99/bd43fe34fc51d1f7fd38fbf7d13d384b to your computer and use it in GitHub Desktop.
Nice, thanks
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();
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.
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?
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
Hi there, do you know where can i put and run this code? Thank you