Question

I have a problem concerning the cross-sell links. I'm building a custom module and I need to add and delete cross-sell links according to a product.

I know how to create the cross-sell links using:

$product->setCrossSellLinkData($params);
$product->save();

But what I don't know is how to remove cross-sell links.

Is there a function to do that or have I to execute an SQL request to delete the links from magentocatalog_product_link inside my module (which isn't a good practice) ?

Thank you for your help

Was it helpful?

Solution

Easiest way:

Get cross sell product ids:

$crossSellLinkData = $product->getCrossSellProductIds();

Flip array so that product ids in the array are key values:

$crossSellLinkData = array_flip($crossSellLinkData);

Then, unset the product you want to remove:

unset($crossSellLinkData[product_id]);

Then set and save:

$product->setCrossSellLinkData($crossSellLinkData);
$product->save();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top