0

Reference to the code https://www.drupal.org/node/1059624#comment-4083864

 $product_type = commerce_product_ui_product_type_new();

  $product_type['type'] = 'prestation';
  $product_type['name'] = t('Prestation');
  $product_type['description'] = t('A basic prestation.');
  $product_type['is_new'] = TRUE;

  commerce_product_ui_product_type_save($product_type, FALSE);

When I execute the folllowing code given above I get a

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'prestation' for key 'PRIMARY': INSERT INTO {commerce_product_type} (type, name, description, help, revision) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4); Array ( [:db_insert_placeholder_0] => abcs_new15 [:db_insert_placeholder_1] => abcs new15 [:db_insert_placeholder_2] => abcs new15 [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => 1 ) in drupal_write_record() (line 7239 of

1

You are getting the error because you are setting $product_type['is_new'] to TRUE, which is saying to the module the product type is new, but that evidently is not true. That is what the error is telling you.

Set $product_type['is_new'] to FALSE, or don't set it at all, and you will not get any error.

If you are programmatically creating new product types, be sure you give to $product_type['type'] a unique value for each of the product types you create.

Reference

  • I am giving a unique value to $product_type['type'] – harshal Apr 16 '15 at 7:18
  • also $product_type['new'] = TRUE since I need to add a new product type programmatically – harshal Apr 16 '15 at 7:18
  • 1
    You are clearly using for $product_type['type'] a value that is already present in the table used by the module, or the database engine would not return an error saying "Duplicate entry 'prestation'." – kiamlaluno Apr 16 '15 at 7:22

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.