0

In our store, we are using custom product URL keys: {manufacturer}-{product-key}, so for example "lg-42lf580v".

Sometimes when I execute some code to update a few products from a script, the product URL key gets reset to the default value without the manufacturer in it, so: "42lf580v".

This only happens when I use the save() method on a product, for example:

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', 'SKU');

if ($product) {                            
        // Update the price.
        $product->setSpecialPrice(200);

        // Set the stock to not allow backorders.
        $stockItem = Mage::getModel('cataloginventory/stock_item');
        $stockItem->assignProduct($product);
        $stockItem->setData('backorders', 0);
        $stockItem->save();

        $product->save();
    }

In this case, I only want to update the backorders value, but still the product URL key gets reset sometimes. Does anyone have any idea what I might be doing wrong here?

4
  • 2
    It might not directly answer your question, but any reason for you to run a product save? $stockItem->save(); should be sufficient. Aug 28, 2015 at 14:03
  • Thanks for your answer, yes you are correct, my example is not sufficient, I extracted this from a script for easier explanation, I'll add some more code.
    – Niels
    Aug 28, 2015 at 14:07
  • Makes more sense now :) What do you use to generate the URL Key? I guess there is some kind of observer on product save? If you could display that code too, it would probably help. I can only imagine it's trying to get the manufacturer but for some reason doesn't have enough info. Aug 28, 2015 at 14:09
  • I'm assuming that you load up $productData here because not all of the attributes load when you use loadByAttribute() but you never actually use $productData Is your original script this way?
    – Tyler V.
    Aug 29, 2015 at 8:07

1 Answer 1

1

To make sure it doesn't happen you can save the URL key on each product save in your script

[...]
$product->setUrlKey($product->getUrlKey());
$product->setUrlKeyCreateRedirect(null);
[...]

That should do the trick

2
  • Thanks, yes that's what I do now, but that's a workaround.
    – Niels
    Sep 3, 2015 at 8:44
  • After changing products name, product url key will not update, i mean url not reset, how can i bulk reset products url key based on products name changes, now i updated 1000 of products name, howc an i bulk reset products url key. Because
    – Gem
    Mar 15, 2018 at 6:46

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.