0

first at all, I'm totally a newby if it regards Magento 2.

I have a PHP script which I run on the command prompt using: php <program-name> What this script does is assign Upsell products programatically.

Hereunder an excerpt:

First, I create the link and push to $linkData array;

  $productLink = $obj->create('Magento\Catalog\Api\Data\ProductLinkInterface')
                ->setSku($productToUpdate->getSku())
                ->setLinkedProductSku($productToAdd->getSku())
                ->setPosition(0)
                ->setLinkType('upsell');
        $linkData[] = $productLink;

After all productLink's are created, I update the upsell products:

$productFinalUpdate = $productRepository->get($productToUpdate->getSku());
$productFinalUpdate->setProductLinks($linkData);
$productRepository->save($productFinalUpdate);

What I'm trying to do is removing all upsell products before reassigning them. However, I have no clue about the following:

How can I remove all upsell products of a product from my program?

What I tried is this:

Check for a method 'unsetProductLinks' or 'removeProductLinks', but no luck

$productFinalUpdate->setProductLinks(null);

$productFinalUpdate->setProductLinks([$anEmptyArray]);

What is the setPosition() doing exactly?

When I assign 0, 1, 2, 3, 4, it doesn't work with there are no upsell products allready assigned. When I assign 0, it adds the upsell products, but only two of them appear in admin weird enough.

Thanks!

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.