2

I am getting the above error on a limited number of products, when loading and saving them with this code:

$product = $productRepo->get($_sku);
$product->setStockData(['qty'=>$new_qty,'is_in_stock'=>($new_qty > 0 ? true : false)]);
$product->save();

I can't identify anything unusual with this (simple) product, but of course something must be amiss, has anyone come across this on their site?

3 Answers 3

1

If Magento is not trying to create a new product, check if your products already don't have the same url key (it must be unique).

0

I ran into this issue when attempting to use System->Import with a CSV file when importing a large number of products. I generated the CSV with a MySQL query and, using this UDF, generated my own URL Key for each product rather than allowing Magento to generate it.

So, my SELECT statement has the following for generating the URL Key:

CONCAT(LOWER(REPLACE(remove_non_alphanum_char_f(TRIM(`Description`)), ' ', '-')), '-', `Internal ID`,FLOOR(10000 + RAND() * 89999)) AS url_key

Working from inside-out:

  • Remove trailing spaces from my Description field using MySQL's TRIM() function
  • Using the UDF ("remove_non_alphanum_char_f()"), remove non-alphanumeric characters by replacing them with hyphens
  • Lowercase the string using MySQL's LOWER() function
  • Using MySQL's CONCAT() function, concatenate the resulting string with my product's Internal ID AND a randomly-generated integer. You may not wish to include your own product ID, BUT THE RANDOMLY-GENERATED INTEGER IS IMPORTANT! It's what'll make your URL Key for this particular product unique from others that might have a similar product name.

There's probably a more-elegant way to do this but it got me over the hump on this recurring URL Key issu. Hope this helps!

0

You should not save your product using $product->save(), but instead you should use the Repository $productRepo->save($product). This is the Magento2 way.

Or alternatively you should be sure that the url_key is loaded into your product before attempting to save. I saw that this error appears when trying to update a product when it's url_key is not among its _data properties.

1
  • productRepo gives an error when no product is found. stuck with factories. Commented Oct 6, 2016 at 16:20

Your Answer

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

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