When trying to replicate Commerce Product exception is thrown, because the column is unique.

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'TCMTV' for key 'sku': INSERT INTO {commerce_product} (revision_id, sku, title, type, language, uid, status, created, changed, data) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array ( [:db_insert_placeholder_0] => [:db_insert_placeholder_1] => TCMTV [:db_insert_placeholder_2] => Test Custom Mirror TV [:db_insert_placeholder_3] => product [:db_insert_placeholder_4] => und [:db_insert_placeholder_5] => 1 [:db_insert_placeholder_6] => 1 [:db_insert_placeholder_7] => 1422365157 [:db_insert_placeholder_8] => 1422377566 [:db_insert_placeholder_9] => b:0; ) в функции drupal_write_record() (строка 7202 в файле /home/webmaster/domains/d7c.ru/avistv.d7c.ru/includes/common.inc).

What I suggest is to add to the end of SKU date of replication.

Comments

sam152’s picture

Status: Active » Needs review

I was using commerce_autosku at the time of development which handled this situation. I could set the SKU to something random but I'm not sure if that is a good default behaviour, given people who aren't automatically generating SKU's probably need them to represent a catalogue in some way.

Here is the patch, not sure if this will make it in.

diff --git a/replicate_commerce.module b/replicate_commerce.module
index 5444b96..6110e74 100644
--- a/replicate_commerce.module
+++ b/replicate_commerce.module
@@ -11,6 +11,7 @@ function replicate_commerce_replicate_entity_commerce_product($entity) {
   $entity->revision_id = NULL;
   $entity->revision_timestamp = NULL;
   $entity->product_id = NULL;
+  $entity->sku = $entity->sku . '_' . rand(10000, 99999);
 }

 /**
artemboiko’s picture

I think need add some string like "replicate_some_hash". What you think about this?

ASGAlex’s picture

Sam152, could you explain your commerce_autosku configuration for succesfully cloned product? I also use commerce_autosku, but also have this error. The patch fixed it.

arefen’s picture

#1 patch fixed my problem.

shahidbscs’s picture

Yeah #1 fix the problem, thank you

gravisrs’s picture

Patch #1 is generally bad approach to add random numbers. After cloning one product from another few times you'd have sku like SHIRT1-1111-2222-333-44-5-666-777-888..... and remember that database field is limited characters so you will eventually stuck with exactly same error after few clones. Moreover some modules actually shows SKU by default (invoice printing) so better to keep them "nice".

For better approach I'd suggest to use name like:
[SKU]-[cloned productid]

Of course first we should try to remove "-[current productid]" part to avoid problem described above.

I know that [cloned productid] isn't available at the moment of cloning so it should be created temporarily (eg [SKU]-temp) and then updated in post-cloning.

Would be easiest if replicate module have post-cloning hooks.

ouissla’s picture

I worked around this issue one a website I'm working on by implementing the following hook in a custom module:

/**
 * Implements hook_replicate_entity_ENTITY_TYPE().
 */
function MYMODULE_replicate_entity_commerce_product($entity) {
  $entity->revision_id = NULL;
  $entity->revision_timestamp = NULL;
  $entity->product_id = NULL;

  $i = 1;
  $base_sku = explode('-', $entity->sku)[0];
  while (commerce_product_load_by_sku($base_sku . '-' . $i)) {
  	$i++;
  }

  $entity->sku = $base_sku . '-' . $i;
}

In my case, the base SKUs have no hyphens, so it works by exploding the sku to make sure I always get the "base" sku, and then increment the SKU until I find a unique value.