cancel
Showing results for 
Search instead for 
Did you mean: 

Removing list of products from up-sells items

SOLVED

Removing list of products from up-sells items

Hi there,

 

I need to remove a list of products from all the up-sells lists; so what I'm doing is checking the IDs of the up-sells products for each product in the database and ideally remove the IDs of the products I don't need anymore and save it.

 

How can I update the up-sells product list or just remove the item I don't want (having its ID)?

 

Part of the code I wrote:

  • $collection is a Mage Module, Mage::getModel('catalog/product')
  • $discontinued is an array which has as key the entity_id of the product
foreach($collection as $p) {
   $upsell = $p->getUpSellProductIds();

   if ( count($upsell) > 0 ) {

      foreach ($upsell as $k => $u) {

         // Check if the entity_id is among the discontinued products
         if ( isset($discontinued[ $u ]) ) {
            // TODO : Remove the discontinued entity from the up-sells list
         }
      }
   }
}
Thanks in advance for your help
Tags (3)
1 REPLY

Re: Removing list of products from up-sells items

The problem I had was related to the way I was trying to save the new data.

Below the right way to save them:

  • setUpSellLinkData is the function to use
  • $upsellToSave is the array I used to formatting the data for the setUpSellLinkData function

 

foreach($collection as $p) {
	$upsell = $p->getUpSellProductIds();
                       
	if ( count($upsell) > 0 ) {
		$toUpdate = false;

		foreach ($upsell as $k => $u) {

        	// Check if the entity_id is among the discontinued products
            if ( isset($discontinued[ $u ]) ) {
            	// Remove the discontinued entity from the up-sells list
				unset($upsell[$k]);
				$toUpdate = true;
			}
		}

		if ($toUpdate) {
			$upsellToSave = array();

            foreach ($upsell as $u)	// Formatting the data
            	$upsellToSave[ $u ] = array('position'=>'');

            try {
				$p->setUpSellLinkData($upsellToSave)->save();
            } catch (Exception $e) {
            	echo "Problem saving up-sell data.\n";
            }
           
    	}
	}
}