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 :-)
1 Answer 1
Try this:
$data = array();
$link = $product->getLinkInstance();
$link->getResource()->saveProductLinks($product, $data, $link::LINK_TYPE_RELATED);
-
Worked perfectly! Had a quick look at the documentation to work out how to add products as well and both work - thanks!Liam Wiltshire– Liam Wiltshire2015年01月22日 15:09:26 +00:00Commented Jan 22, 2015 at 15:09