Skip to content

Instantly share code, notes, and snippets.

@mrkhoa99
Created September 15, 2016 06:07
Show Gist options
  • Save mrkhoa99/bd43fe34fc51d1f7fd38fbf7d13d384b to your computer and use it in GitHub Desktop.
Save mrkhoa99/bd43fe34fc51d1f7fd38fbf7d13d384b to your computer and use it in GitHub Desktop.
Set related products programatically Magento 2
@Anxiro
Copy link

Anxiro commented Sep 19, 2018

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.

@Aman4111
Copy link

Aman4111 commented Jun 15, 2021

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?

@anhnhan1997
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment