Run Some Code To Update A Field While Product Creation

HI I have written some code which generates an sku code for each product, now the issue is this code is only running once the product has been created and if i save it again ( not when the product is created)

I am using the hook as below

function fn_my_changes_update_product_pre(&$product_data, &$product_id, $lang_code, $can_update){
    if (!isset($product_data['ff_sku'])) 
    {
    	$product_data['ff_sku'] = '';
    }
if($product_data['ff_sku'] == ''  && $product_id >0){
    $product_company_id = db_get_field('SELECT company_id FROM ?:products WHERE product_id = ?i', $product_id);
    $ff_sku = 'ID'.$product_company_id.'-'.$product_id;
    $product_data['ff_sku'] = $ff_sku;
}

}

What i want is that the product sku should get generated when some one creates the product itself in first save itself, and he shouldnt need to save it again.

In my case 2nd save button press is required to generate this sku code.

Add the following code:

if (empty($product_id)) {
    ... your code here..
}

product_id is empty at the moment when the product is created

But that fails my logic then which i am using for making the SKU id if product_id is empty.

        $ff_sku = 'ID'.$product_company_id.'-'.$product_id;

What i want is create the product and then get the product id as in my query and then make the sku field.

In this case you can check if the sku is created already (suppose that it is stored in the cscart_products table)

$sku_exists = db_get_field("SELECT ff_sku FROM ?:products WHERE product_id = ?i", $product_id);
if (empty($sku_exists)) {
    .... your code here ....
}

I am already checking that in the condition

    if($product_data['ff_sku'] == ''  && $product_id >0){

}

Hello!

Try to use this hook:

fn_set_hook('update_product_post', $product_data, $product_id, $lang_code, $create);

Here $create variable will be true if product is being created.

Already treied this

Hello!

Try to use this hook:

fn_set_hook('update_product_post', $product_data, $product_id, $lang_code, $create);

Here $create variable will be true if product is being created.

Already tried this doesnt work as expected. Have to Save the product again to add the SKU

Already treied this

Already tried this doesnt work as expected. Have to Save the product again to add the SKU

What condition do you use?

I am already checking that in the condition

    if($product_data['ff_sku'] == ''  && $product_id >0){

}

Your code checks if the sku exists in the request data

Our code checks if the sku was already added to the database

Compare both solutions to receive desired result

Hope it makes sense

Aah i understand what you mean. Let me try and get back

Your code checks if the sku exists in the request data

Our code checks if the sku was already added to the database

Compare both solutions to receive desired result

Hope it makes sense

Tried it dint work. It creates an sku as : ID-

If i press save again then it works. Which was already the case with my code.

Please try

function fn_my_changes_update_product_pre(&$product_data, &$product_id, $lang_code, $can_update){
    if (!empty($product_id)) {
        $sku_exists = db_get_field("SELECT ff_sku FROM ?:products WHERE product_id = ?i", $product_id);
        if (empty($sku_exists)) {
            $product_company_id = db_get_field('SELECT company_id FROM ?:products WHERE product_id = ?i', $product_id);
            $product_data['ff_sku'] = 'ID' . $product_company_id . '-' . $product_id;
        }
    }
}