Prevent Duplicate Product Code

hi :grin:

i have more than 10000 products :shock: in my cs-cart website ,each one of them has product code :confused: , i want to prevent duplicate product code when admin insert new product with product code already used with other product :confused: ????

Hello abresmos,


You can check unique sku add-on https://store.webkul.com/CS-Cart-Unique-Product-SKU-Or-Code.html



Thank you

Himansu Dangwal

Hello abresmos,

If the question is still relevant, we will be glad to develop this modification for you. Please, contact us.

Best regards,

Alt-team

Please use the update_product_pre hook (app/functions/fn.catalog.php) and 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;
    }
}

or add this code after the following line:

fn_set_hook('update_product_pre', $product_data, $product_id, $lang_code, $can_update);

Please use the update_product_pre hook (app/functions/fn.catalog.php) and 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;
    }
}

or add this code after the following line:

fn_set_hook('update_product_pre', $product_data, $product_id, $lang_code, $can_update);

thanks so much , it's working perfectly

You are welcome!

Same requirement here. Just to clarify.

If the code below is not present in app/functions/fn.catalog.php

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;
}
}

then I should I simply add it under

fn_set_hook('update_product_pre', $product_data, $product_id, $lang_code, $can_update);

so result should be:-

fn_set_hook('update_product_pre', $product_data, $product_id, $lang_code, $can_update);
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;
}
}

The above won't work for import.

Why not just make an index in the cscart_products table making product_code unique? I.e. any new insert (or update of a product) using a duplicate product_code would simply fail at the SQL level.

Would I then get an error message if I tried to save a duplicate product code ?

The SQL INSERT/UPDATE would fail. However, for import/export, cs-cart ignores SQL errors and does not report them. In the UI, you'd get the failure. Try it. If you don't like how it behaves (versus silently setting "$can_update = false") then you can simply drop the index and go back to how you are or using the hook above.

Same issue here! When Im trying to clone a product im gettin error page 'SERVICE

UNAVAILABLE', So what I have to do? We need a lot of times to clone products but atm we create new products with diif pid
Thanks! :)

Note that if you have made the product_code column unique, you will have to make adjustments to the clone code to alter the product code like it does for page titles, product names, etc. by probably adding "[CLONE}" to it.

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

Please use the update_product_pre hook (app/functions/fn.catalog.php) and 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;
    }
}

or add this code after the following line:

fn_set_hook('update_product_pre', $product_data, $product_id, $lang_code, $can_update);

Thanks eCom-Labs for your contribution. However, this doens't work on my store - MV. Is it no longer working for latest MV editions? Or would it be a cache issue (I don't know if PHP files are being cached).

Hmm.. It works on my local installation with the latest Multi-Vendor version

Yes i try mv.4.9.3.sp1 in local. it works. if you try clone the product it don't works yess you are right alaa

Maybe you add a new product in store this works when import products if you want to see live you test TIME code not the other

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);
}
}

Hmm.. It works on my local installation with the latest Multi-Vendor version

That's weired! I tried updating the fn.catalog.php directly with no luck. The results are the same as I have NOT added any code. In fact, I have set the variable $can_update in the beginning of the function (line 2495) from true to false just for checking. Still everthing works as I have dont nothing!

    $can_update = false;

That's weired! I tried updating the fn.catalog.php directly with no luck. The results are the same as I have NOT added any code. In fact, I have set the variable $can_update in the beginning of the function (line 2495) from true to false just for checking. Still everthing works as I have dont nothing!

Did you set the code after hook? 3rd party addons can override it and set to true somewhere

Did you set the code after hook? 3rd party addons can override it and set to true somewhere

Things I have done:

1- Disabled all third party addons.

2- Added return false; just after the original function, like this:

function fn_update_product($product_data, $product_id = 0, $lang_code = CART_LANGUAGE)
{
    return false;
    $can_update = false;

3- Restarted NGINX

4- Block cache is disabled and cleared.

5- Tried on both production server; where I am using redis and apcu, and developement server (no redis and no apcu). Both are not working either.

6- I tried to clear OPcache by executing this command opcache_reset(); in a new php file in CS-Cart root folder, still no good results.

Maybe I tried other things but cannot remember.

It must be cache, but which and where I don't know!! these trivial issues make me crazy.

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