0

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

1

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.