0

I'm trying to programmatically manipulate the product relations in a Magento store.

From what I've read, setRelatedLinkData should be the way to go.

As I simple test, I'm just trying to replace a products related products with nothing (i.e. an empty array), however it's not working - the product in question is still showing the related product in the backend.

The test code I'm working with is:

 $product = Mage::getModel('catalog/product')->load($product->getId());
 $linkData = array();
 print_r($linkData);
 $product->setRelatedLinkData($linkData);
 echo "Save\n";
 $r = $product->save();

As mentioned above however the product still has a related product when I reload it in the backend.

NOTE: I don't only want to remove related products, eventually I want to be able to add new ones as well, so a DELTE FROM... SQL query isn't what I am looking for. However if I can't get it to work to remove products, then it's certainly not going to work to add them, so one step at a time :-)

asked Jan 22, 2015 at 13:44

1 Answer 1

0

Try this:

$data = array();
$link = $product->getLinkInstance();
$link->getResource()->saveProductLinks($product, $data, $link::LINK_TYPE_RELATED);
answered Jan 22, 2015 at 14:46
1
  • Worked perfectly! Had a quick look at the documentation to work out how to add products as well and both work - thanks! Commented Jan 22, 2015 at 15:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.