3

I have a script that updates stock and price but it seems to save all attributes with a default value back to the default.

The collection is loaded like this:

$productsCollection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect(array('visibility','name','price','plu'))
            ->addAttributeToFilter('sku', array('in' => $this->skuArray));

foreach ($productsCollection as $product)
        { 
            //update stuff here
            $product->save();

        }

I have read somewhere that using load() is needed to save only those attributes specifically selected but if I do that I get a memory exhausted error.

Can someone give me some pointers on how to modify what I have in order to only save the attributes I have selected?

1 Answer 1

1

After some more investigation I solved it.

save() will save all attributes and because no value was given for those attributes with default values they were getting reset back to their default values.

Replacing $product->save() with $product->getResource()->saveAttribute($product, 'attribute_name') saves only those attributes and leaves all others untouched.

As an added bonus the script runs a lot quicker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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