Import products - import new field to product DB info

I have one addon that creates new product column name (i.e. “new_field” ) in DB Table: cscart_products - product table , that a value can be filled manually via each product extra field found on admin/vendor product details add-on tab. The thing is that I need to save time and autofil that field via import feed table content from the feed. I.e. xml column “new_field” content for each product to go into DB cscart_products - product table - “new_field”- value. So I need that “new field” to be selected as a destination for that feed column. This way I can import the info from the feed into the new extra field added by my addon on product DB info.

How can I achieve that ? What cs-cart php file I need to edit and how ?

In admin area - addon tab , the new field added to the product have the ID (found in source code of the page ): “new_field”.

I managed to solve it but the import for the mentioned field won’t get updated on import update /cron job, it only import the “new_field” if I delete the vendor products and do the import them again. How to fix it so it can updated the new data on next import ?

You should extend the following schema to add new field for export/import

app/schemas/exim/products.php

2 Likes

I should add this to app/schemas/exim/products.php ?

'new_field' => [
            'db_field'    => 'new_field',
            'table'       => 'cscart_products',
              'multilang'   => true,
              'process_get' => ['fn_export_product_descr', '#key', '#this', '#lang_code', 'new_field'],
              'process_put' => ['fn_import_product_descr', '#this', '#key', 'new_field', '#new'],
        ]

I can see function fn_export_product_descr run under products.functions.php file , should I replace above 'fn_import_product_descr" with ‘fn_import_new_field’ and add a function on products.functions.php ? If so, how it should look like ,like this (I tried to edit existing price import function) ?

function fn_import_new_field($product_id, $new_field, $is_create, array $object = [], $store = '')
{
    if (fn_allowed_for('ULTIMATE')) {
        if (fn_ult_is_shared_product($product_id) == 'Y') {
            if (!($company_id = Registry::get('runtime.company_id'))) {
                $company_id = fn_get_company_id_by_name($store);
            }
            fn_update_product_new_field($product_id, ['new_field' => $new_field, 'create' => $is_create], $company_id);
        }
    }
    // Update  new_field only if object is not a product that is shared for current storefront
    if (isset($object['is_shared_product'])) {
        return;
    }
    fn_update_product_new_fields($product_id, ['new_field' => $new_field, 'create' => $is_create]);
}

Thank you @ecomlabs !

Hi!

If it is a simple field in the cscart_products table, you need to extend the app/schemas/exim/products.php scheme in the following way:

$schema['export_fields']['New field'] => [
    'db_field' => 'new_field'
];

return $schema;

If I add the line on app/schemas/exim/products.php I get service unavailable error on domain. Should I add to my addon folder path that generated the cscart_products DB field , i.e. /app/addons/myaddon/schemas/exim/products.php ? or maybe in ‘my_changes’ addon /app/addons/my_changes/schemas/exim/products.php ?

Also is it $scheme or $schema ?

Thanks for pointing that out, it is definitely $schema.

I gave you an example of extending the schema, not changing the default file. Please see the article on extending schemes. Here’s the link to the documentation if you missed it in my previous message:
https://docs.cs-cart.com/latest/developer_guide/addons/scheme_extending.html

So your example work on both direction import/import , right ?

$schema['export_fields']['New field'] => [
    'db_field' => 'new_field'
];

return $schema;

I added it to /app/addons/my_changes/schemas/exim/products.php

Just use

/app/addons/my_changes/schemas/exim/products.post.php

instead of

/app/addons/my_changes/schemas/exim/products.php

And do not forget to clear cache

But before you answered the first time , I already added some content on /app/addons/my_changes/schemas/exim/products.post.php and works, import takes place correctly getting the new DB field filled on very product but only on first import, If the cron runs and that specific field is changed by the vendor, it will not update/reflect on DB. Here is what I used on /app/addons/my_changes/schemas/exim/products.post.php

<?php
$schema['export_fields']['new_field'] =  array(
      'db_field' => 'new_field'
);
return $schema;

Should I replace this with your code and both issues will be solved (initial import and future imports to update the DB field if vendors is changing the speicific feed field content) ?

<?php
$schema['export_fields']['new_field'] => [
    'db_field' => 'new_field'
];

return $schema;

I’m using this on CS Cart Multi-Vendorv4.15.2 and works fine. If I do the same exact thing on Multi-Vendorv4.17.2.SP1 by creating products.post.php with the same content in /app/addons/my_changes/schemas/exim/ , it fail to work. The new_field won’t show on Product property selection field in import products. I cleared cache with no result :frowning: How to fix it ? Did I miss any step beside this ?

What about old products import? Do you see this field?

Its another domain with fresh cs-cart install.

It works for me in a clear 4.17.2 installation, so I don’t know why it doesn’t work for you :frowning:

Is the My changes addon installed and active on Multi-Vendorv 4.17.2.SP1?

3 Likes

:man_facepalming: That was it… thank you so much!

I need to import that field in 2 database fields, new_field_1 and new_field_2.

It will look like that ?

<?php
$schema['export_fields']['new_field_name'] => [
    'db_field' => 'new_field1';'db_field2' 
];

return $schema;

No it cannot be done automatically via scheme. You have to create a function that will update the second field (is it really necessary?) and define it in the process_put element. For example:

<?php
$schema['export_fields']['new_field_name'] => [
    'db_field' => 'new_field1',
    'process_put' => ['fn_my_changes_import_new_field, '#key', '#this']
];

return $schema;
function fn_my_changes_import_new_field($product_id, $value)
{
    db_query('UPDATE ?:table SET field = ?s WHERE product_id = ?i',  $value, $product_id);
}

Thank you but I don’t see db_field2 name on you code. When ```
db_field1 is imported , I want to replicated the product db_field1 content -on product DB field db_field2.

Please try

<?php
$schema['export_fields']['new_field_name'] => [
    'db_field' => 'new_field1',
    'process_put' => ['fn_my_changes_import_new_field, '#key', '#this', 'new_field2']
];

return $schema;
function fn_my_changes_import_new_field($product_id, $value, $second_field = '')
{
    db_query("UPDATE ?:table SET field = ?s WHERE product_id = ?i",  $value, $product_id);
    if (!empty($second_field)) {
        db_query("UPDATE ?:table SET $second_field = ?s WHERE product_id = ?i",  $value, $product_id);
    }
}