0

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 upsells product list or just remove the item I don't want (having its ID)?

Just to avoid some misunderstanding of the problem, below part of the code I wrote:

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 upsells list
         }
      }
   }
}
  • You want to remove upsell products part from product page? – Chirag Rajput Aug 10 '16 at 10:35
  • If you want to bulk edit upsell then you might take a look at github.com/tzyganu/ProductRelationsImport – Arjun Aug 10 '16 at 10:37
  • No, updating the upsell list in the administration area, just instead of doing it manually I'm creating a script which is using the catalog/product Model -- Mage::getModel('catalog/product') – Luigi T. Aug 10 '16 at 10:38
0

You are going in right direction. You can do it like this:

Get upsell product ids:

$upSellLinkData = $product->getUpSellProductIds();

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

$upSellLinkData = array_flip($upSellLinkData);

Then, unset the product you want to remove:

unset($upSellLinkData[product_id]);

Then set and save:

$product->setUpSellLinkData($upSellLinkData);

$product->save();

Please let me know if you find any problem.

You can use above codes in your loop to do it for multiple products.

Reference link: Delete cross-sell links programmatically

  • Is not saving the changes.. The code is not giving any error, but the upsells list still the same. – Luigi T. Aug 10 '16 at 12:04
  • Please see my updated answer. – Mohit Kumar Arora Aug 10 '16 at 12:22
  • Still the same, I guess the method 'setUpSellProductIds' doesn't exist :/ – Luigi T. Aug 10 '16 at 12:37
  • I have not mentioned setUpSellProductIds in my code. Please recheck. – Mohit Kumar Arora Aug 10 '16 at 12:49
  • Yeah, I was looking at the previous version. The method setUpSellLinkData is the right one, just the way is formatted the array is not the one accepted by that method -- See the final version of the code magento.stackexchange.com/a/130802/40598 – Luigi T. Aug 10 '16 at 13:03
0

You can use below sql query :

 DELETE l.* FROM catalog_product_link as l
 LEFT JOIN catalog_product_link_type as lt ON (lt.`link_type_id` = l.`link_type_id`)
 WHERE lt.code = "up_sell"

Note::Take Database Backup Before doing any database operation.

Below is Magento Code for remove fix upsell item from all products:

 $collection = Mage::getModel('catalog/product')->getCollection();

foreach($collection as $p) {
    $p = Mage::getModel('catalog/product')->load($p->getId());
    $upsell = $p->getUpSellProductIds();

    //print_r($upsell);

    $upSellLinkData = array_flip($upsell);

    unset($upSellLinkData[PRODUCT_ID]); - PRODUCT_ID - This is your product id you want to remove from all products 

    //$upSellLinkData = array_flip($upSellLinkData);

    //print_r($upSellLinkData);

    $p->setUpSellLinkData($upSellLinkData);
    $p->save();
    //print_r($upSellLinkData);
}
  • I'd prefer doing it using the Magento functions. I've added the part of the code I'm stuck with – Luigi T. Aug 10 '16 at 11:00
  • Please see updated code. – Chirag Rajput Aug 10 '16 at 11:29
  • I tested the code but seems the changes are not applied.. The up-sell list is the same. – Luigi T. Aug 10 '16 at 12:13
0

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 funciton

    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";
            }
        }
    }
    }
    

Just pasted the code here http://pastebin.com/sMeydZ9A

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.