问题描述
当我尝试以编程方式设置相关、追加销售和交叉销售产品时。 所以它第一次添加了相关产品,而不是追加销售产品。在这种情况下,我再次设置了产品数据,然后设置了追加销售,但会自动删除旧的相关产品集。
这是以下代码:-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productObject = $objectManager->get('Magento\Catalog\Model\Product');
$product_new = $productObject->loadByAttribute('sku',$sku);
$linkDataAll = [];
$skuLinks = "Ring1014,Ring10155";
$skuLinks = explode(",",$skuLinks);
foreach($skuLinks as $skuLink) {
//check first that the product exist
$linkedProduct = $productObject->loadByAttribute("sku",$skuLink);
if($linkedProduct) {
//The setLinkType() can be tree types “related”,“crosssell”,“upsell”,customlinked,accessory
/** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLinks */
$productLinks = $objectManager->create('Magento\Catalog\Api\Data\ProductLinkInterface');
$linkData = $productLinks //Magento\Catalog\Api\Data\ProductLinkInterface
->setSku($product_new->getSku())
->setLinkedProductSku($skuLink)
->setLinkType($links);
$linkDataAll[] = $linkData;
}
}
if($linkDataAll) {
//print(count($linkDataAll)); //gives 3
$product_new->setProductLinks($linkDataAll);
}
$product_new->save();
那么你能告诉我我在代码中做错了什么吗。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)