0

If I add upsells products code above related product then only related products importing and If I add related products code above upsells product then only upsells products importing.
This code running in foreach loop

upsell code -> No Import
related code -> Import

related code -> No Import
upsell code -> Import

But I want to add both products. How to solve code position problem?
Any have a solution for that?

/*upsell*/
    $product->load($product->getId());
    $linkDataAlls = [];
    $skuLinksup = "sj-2200,marbs,spoon,spoon-1,spoon-1-1,spoon-1-1-1";
    $skuLinksup = explode(",",$skuLinksup);
    foreach($skuLinksup as $skuLinkup) {
        $linkedProducts = $product->loadByAttribute("sku",$skuLinkup);
        if($linkedProducts) {
        $productLinks = $objectManager->create('Magento\Catalog\Api\Data\ProductLinkInterface');
        $linkDatas = $productLinks
            ->setSku($product->getSku())
            ->setLinkedProductSku($skuLinkup)
            ->setLinkType("upsell");
        $linkDataAlls[] = $linkDatas;
        }
    }
    $product = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->get($product->getSku());
    if($product) {
        $product->setProductLinks($linkDataAlls);
    }
    $product->save();
    /*upsell*/


    /*related*/
    $product->load($product->getId());
    $linkDataAll = [];
    $skuLinks = "sj-2200,marbs,spoon,spoon-1,spoon-1-1,spoon-1-1-1";
    $skuLinks = explode(",",$skuLinks);
    foreach($skuLinks as $skuLink) {
        $linkedProduct = $product->loadByAttribute("sku",$skuLink);
        if($linkedProduct) {
        $productLink = $objectManager->create('Magento\Catalog\Api\Data\ProductLinkInterface');
        $linkData = $productLink
            ->setSku($product->getSku())
            ->setLinkedProductSku($skuLink)
            ->setLinkType("related");
        $linkDataAll[] = $linkData;
        }
    }
    $product = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->get($product->getSku());
    if($product) {
        $product->setProductLinks($linkDataAll);
    }
    $product->save();
    /*related*/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.