Magento – Save Upsell Products Performative

If you would like to change some attributes of a product in Magento, you can do this by using the appropriate setter and the save() method:

$product = Mage::getModel('catalog/product')->load($id);
$product->setAttribute('attribute value');

If you only change one or a few attributes, this method is not very performant because it saves every attribute of the product object. As the guys from Flagbit described in their blog, there is a much more performant method to save single EAV attributes:

$product = Mage::getModel('catalog/product')->load($id);
$product->setAttribute('attribute value');
$product->getResource()->saveAttribute($product, 'attribute');

Anyway, this will not work if you like to save new upsell products. Fortunately, there is another performant method in order to assign upsell products:

// array with product IDs as key and the position as value
$upsellProductsArray = array(154 => array('position' => 0), 155 => array('position' => 1));
$product->getLinkInstance()->_getResource()->saveProductLinks($product, $upsellProductsArray, Mage_Catalog_Model_Product_Link::LINK_TYPE_UPSELL);

This works similar for related and cross sell products. If you would like to save all of them – related, upsell and cross sell products, have a look at Mage_Catalog_Model_Product_Link::saveProductRelations.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *