Prevent Duplicate Product Code

I made the same action and see that the product was not created. If you want to add error notification, use the following code

if (empty($product_id)) {
    $pid = db_get_field("SELECT product_id FROM ?:products WHERE product_code = ?s", $product_data['product_code']);
    if (!empty($pid)) {
        $can_update = false;
        fn_set_notification('E', __('error'), __('unique_sku_error'));
    }
}

Then add unique_sku_error language variable with the required value

You are right, eComLabs.

I get it working finally. The error notification is very handy, thank you very much.

Best Regards,

Alaa

You are right, eComLabs.

I get it working finally. The error notification is very handy, thank you very much.

Best Regards,

Alaa

If you like to find products with duplicate product codes you can use our addon - Incomplete products.

Please try

if (empty($product_id)) {
    $pid = db_get_field("SELECT product_id FROM ?:products WHERE product_code = ?s", $product_data['product_code']);
    if (!empty($pid)) {
        $product_data['product_code'] .= ('_' . TIME);
    }
}

In this case timestamp value will be added to products with duplicated sku

Hello,

I want to add the product id value for the sku. So instead of timestamp, I want to get the value of the id for the same product.

Thanks in advance.

Why wouldn't you just create an index in the products table to make product_code unique? Cs-cart might even generate an error when it can't update the product because of a unique index violation... Or it should be in the logs for database.

Hello,

I want to add the product id value for the sku. So instead of timestamp, I want to get the value of the id for the same product.

Thanks in advance.

Product ID is not known yet. So you should add product with duplicated sku. Then get product ID and change product code

Why wouldn't you just create an index in the products table to make product_code unique? Cs-cart might even generate an error when it can't update the product because of a unique index violation... Or it should be in the logs for database.

Product ID is not know yet. So you should add product with duplicated sku. Then get product ID and change product code

Thank you everybody for your replies.

I did try this:

if (empty($product_id)) {
    $pid = db_get_field("SELECT MAX(product_id) FROM ?:products");
    $new_pid= $pid +1;
    $product_data['product_code'] = $new_pid;

}

However, the above didn't work when adding new product after deleting one.

I am thinking of having a static value for the product code when creating a product. Then, getting the product id for that specific sku and change the value of the sku to the product id.

Regards,

Alaa

Old post but I have some issues related to duplicate products, when I start the import from feed and select 'update only" it will created new product with exact same product code even if I select on import map only product code and images (to import only images for existing products). I thought that in such case it will search and find the existing product code on feed file and update images or whatever field I select beside product code. Why is this happening and how to fix it ?

This needs examination in your store. Please contact us via Help Desk on this case.