I'm not sure if this is even possible but say for example i have a category called X with 100 products in it, can i add say 3 products to all them 100 products in that particular category without manually going through every single product selecting which product(s) i would like to cross sell.
1 Answer
You can try below code:
$categoryId= 123;
$categoryProducts = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();
foreach($categoryProducts as $product){
$param = array();
$crosssells = $product->getCrossSellProducts();
foreach ($crosssells as $item) {
$param[$item->getId()] = array('position' =>$item->getPosition());
}
$newCrossSellProductsIds=array(4,5,6);
foreach ($newCrossSellProductsIds as $productID) {
if (!isset($param[$productID])){
$param[$productID]= array(
'position'=>1
);
}
}
$product->setCrossSellLinkData($param);
$product->save();
}