Send Data To Database

Hello friends.
I read the CS-Cart codes and functions, but found functions that were just for get data and use.
I created an add-on for post a new product. How do I send data to the database? Information like: Product information in the cs-cart_product table, category information selected. Selected features information.
Thanks for help me.

Hello

Function fn_update_product who located in file app/functions/fn.catalog.php

Best regards

Robert

Hello

Function fn_update_product who located in file app/functions/fn.catalog.php

Best regards

Robert

Hello
thanks for the answer
Is this function used to send data or update it?

Hello

This function creates or updates the product depending on the parameters.

Best regards

Robert

Hello

This function creates or updates the product depending on the parameters.

Best regards

Robert

Thanks Robert

Hello

You are welcome :)

Best regards

Robert

Minimum amount of parameters:

- product (product name)

- price

- category_ids (array with IDs of categories)

- status

- company_id (ID of store-front or vendor)

Minimum amount of parameters:

- product (product name)

- price

- category_ids (array with IDs of categories)

- status

- company_id (ID of store-front or vendor)

thanks bro. You always helped me. Thank you. :wub: :wub:

Another question? How do I send selected features?
What is the parameter for the features?

All the information I want to send is as follows.
Û±. Product Name.
Û². Category (Only one category should be selected.)
Û³. Selected features.
Û´. Price
Ûµ. currency
Û¶. I added a column in the cs-cart_products table called the video_link. I want to add data to this column.

Another question? How do I send selected features?

It depends on the feature type. I suggest you to do the following

- open the /app/controllers/backend/products.php file

- add

fn_print_die($_REQUEST['product_data']);

after

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

- add test product with selected features and check the product_features element of the array which will be printed in the screen

hi my friend...

I got the parameters with this code: fn_print_die(). But there is a problem.

We had a page to post a new product but wanted to change it ... I changed, it but error when clicking send: Sorry, service is temporarily unavailable.
The current page was like all the information you received was in one place ... that is, you must enter the product name first and then the category. After selecting the category there, I think using AJAX it would display the relevant features of that category.

But I want to choose a category before anything else ... I did. And on another page I got the category and sent it to the product insertion page with parameter: category_id.

But the features of that category were not displayed. In PHP code I copied the part that was to get features and add it to another part. This will show up after sending the ID to the related features category. But an error occurred after clicking send. I looked to see that catgory_id could not be sent, so I added it to the array using session but it still made an error when registering.

all codes:

if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {
$suffix = ‘manage’;
if ($mode == ‘update’) {
$suffix = ‘add’;
fn_save_post_data(‘wk_product_data’);
$auth = Tygh::$app[‘session’][‘auth’];
$wk_product_data = $_REQUEST[‘wk_product_data’];
if ($wk_product_data) {
// Registry::set(‘runtime.company_id’, $auth[‘company_id’]);
list($product_ids, $paid_product_ids, $paid_products_cost, $original_cost) = Fn_Create_Wk_Custom_product($wk_product_data, $auth);
if (empty($product_ids)) {
return array(CONTROLLER_STATUS_REDIRECT,‘wk_custom_product.add’);
}
fn_restore_post_data(‘wk_product_data’);
if ($paid_product_ids && $paid_products_cost && $paid_products_cost>0) {
fn_define(‘ORDER_MANAGEMENT’, true);
$cart = &Tygh::$app[‘session’][‘cart’];
$auth = &Tygh::$app[‘session’][‘auth’];
$vendor_id = $auth[‘company_id’];
fn_clear_cart($cart, true);
$cart[‘user_data’] = fn_get_user_info($auth[‘user_id’], true, $cart[‘profile_id’]);
$cart[‘is_custom_order’] = ‘Y’;
$product_id = Fn_Wk_Custom_Product_Get_Custom_product();
$product_extra = array(
‘paid_product_ids’ => $paid_product_ids
);
fn_add_product_to_cart(
array(
$product_id => array(
‘product_id’ => $product_id,
‘amount’ => 1,
‘price’ => $paid_products_cost,
‘list_price’ => $original_cost,
‘product_options’ => array(),
‘stored_price’ => ‘Y’,
‘stored_discount’ => ‘Y’,
‘discount’ => 0,
‘company_id’ => 0,
‘extra’ => $product_extra,
),
),
$cart,
$auth
);

            list($status, $redirect_params) = fn_checkout_update_steps(
                $cart, $auth, array(
                    'update_step' => 'step_three',
                    'next_step'   => 'step_four',
                )
            );

            return array(
                CONTROLLER_STATUS_REDIRECT,
                Url::buildUrn(array('checkout', 'checkout'), $redirect_params),
            );
        }
        return array(CONTROLLER_STATUS_REDIRECT,'wk_custom_product.manage');
    }
}

if ($mode == 'wk_update') {
    fn_save_post_data('wk_product_data');
    $wk_product_data = $_REQUEST['wk_product_data'];
    $product_id = $_REQUEST['product_id'];
    if ($wk_product_data && $product_id) {
        if (isset($wk_product_data['images'])) {
            Fn_Create_Product_Uploadable_image($wk_product_data['images'], $product_id);
        }
        $wk_product_data['category_ids'] = array($wk_product_data['category_ids']);
        if(isset($wk_product_data['custom_product_data']))
            unset($wk_product_data['custom_product_data']);
        if(isset($wk_product_data['custom_product_data']))
            unset($wk_product_data['custom_product_data']);
        if (isset($wk_product_data['product_features'])) {
            $price_feature_id = Registry::get('addons.wk_custom_product.product_price_features');
            $currency_feature_id = Registry::get('addons.wk_custom_product.product_currency_features');
            $online_payment_feature_id = Registry::get('addons.wk_custom_product.online_product_features');
            $product_features = $wk_product_data['product_features'];
            if (isset($product_features[$online_payment_feature_id])) {
                $wk_product_data['is_online_product'] = $product_features[$online_payment_feature_id] == 'Y'?'Y':'N';
            }
            if (isset($product_features[$price_feature_id])) {
                $wk_product_data['price'] = $product_features[$price_feature_id];
            }
            if (isset($product_features[$currency_feature_id])) {
                $wk_product_data['currency'] = $product_features[$currency_feature_id];
            }
        }
        if (Registry::get('addons.sd_google_translate_api.gt_api_events.update_entity_via_website') == 'Y' || Registry::get('addons.sd_google_translate_api.gt_api_events.create_entity_via_website') == 'Y') {
            $create = empty($_REQUEST['product_id']) ? true : false;
            Registry::set('gt_api_update_product', $create);
        }

        $product_id = fn_update_product($wk_product_data, $product_id);

        fn_wk_custom_product_assign_urgent_badge($product_id);

        fn_restore_post_data('wk_product_data');
    }
    $suffix = 'wk_edit?product_id=' . $product_id;
}

if ($mode == 'calculate_price') {
    $all_product_data = $_REQUEST['all_product_data'];
    $selected_product_data = $_REQUEST['selected_product_data'];
    list($all_product_price, $selected_product_price) = Fn_Wk_Calculate_Plan_price($all_product_data, $selected_product_data);
    Tygh::$app['view']->assign('value', $all_product_price);
    $res = Registry::get('view')->fetch('common/price.tpl');
    Registry::get('ajax')->assignHtml("wk_all_product_plan_price_total", $res);
    $id= $_REQUEST['result_ids'];
    Tygh::$app['view']->assign('value', $selected_product_price);
    $res = Registry::get('view')->fetch('common/price.tpl');
    Registry::get('ajax')->assignHtml("$id", $res);
    exit;
}

if ($mode == 'upload_image') {
    echo Fn_Upload_Custom_Product_Temp_images();
    exit;
}

if ($mode == 'delete_image') {
    $product_id = $_REQUEST['product_id'];
    $pair_id = $_REQUEST['pair_id'];
    fn_delete_image_pairs($product_id, 'product', '', array($pair_id));
    exit;
}

//
// Delete product
//
if ($mode == 'delete') {
    if (!empty($_REQUEST['product_id'])) {
        $result = fn_delete_product($_REQUEST['product_id']);
        $suffix = 'manage';
        if ($result) {
            fn_set_notification('N', __('notice'), __('text_product_has_been_deleted'));
        } else {
            $suffix = 'wk_edit?product_id=' . $_REQUEST['product_id'];
        }
    }
}
return array(CONTROLLER_STATUS_REDIRECT, 'wk_custom_product.'.$suffix);

}

if ($mode == ‘category’) {

$auth = Tygh::$app['session']['auth'];
if (empty($auth['user_id'])) {
    return array(CONTROLLER_STATUS_REDIRECT, 'auth.login_form?return_url=' . urlencode(Registry::get('config.current_url')));
}
if (empty($auth['company_id']) || $auth['user_type'] != 'V') {
    fn_set_notification("W", __("warning"), __("before_post_an_add_register_yourself_as_publisher"));
    return array(CONTROLLER_STATUS_REDIRECT,'companies.vendor_plans');
}

if(!isset($_REQUEST['category_id']) || empty($_REQUEST['category_id']) || $_REQUEST['category_id'] == '0'){

    Registry::get('view')->assign('get_category', true);

}elseif(isset($_REQUEST['category_id']) && !empty($_REQUEST['category_id'])) {

    Registry::get('view')->assign('sub_category', true);
    Registry::get('view')->assign('sub_category_picker', $_REQUEST['category_id']);

}

}

if ($mode == ‘health_beauty’) {

}

if ($mode == ‘add’ || $mode == ‘wk_edit’) {

$auth = Tygh::$app['session']['auth'];
if (empty($auth['user_id'])) {
    return array(CONTROLLER_STATUS_REDIRECT, 'auth.login_form?return_url=' . urlencode(Registry::get('config.current_url')));
}
if (empty($auth['company_id']) || $auth['user_type'] != 'V') {
    fn_set_notification("W", __("warning"), __("before_post_an_add_register_yourself_as_publisher"));
    return array(CONTROLLER_STATUS_REDIRECT,'companies.vendor_plans');
}
fn_add_breadcrumb(__("wk_post_an_ad"));
$discount_data = Fn_Get_Custom_Product_Plan_Pricing_discount();
Registry::get('view')->assign('discount_data', $discount_data);
if ($mode == 'add') {
    $wk_product_data = fn_restore_post_data('wk_product_data');
    if ($wk_product_data) {
        // if(!isset($wk_product_data['product']))
            // Registry::get('view')->assign('wk_product_data_list', $wk_product_data);
    }
    $wk_custom_product_type_data = Fn_Get_Custom_Product_Type_data();
    if ($wk_custom_product_type_data) {
        Registry::get('view')->assign('wk_pricing_data', $wk_custom_product_type_data['pricing_data']);
        $wk_custom_product_data = isset($wk_custom_product_type_data[DESCR_SL])?$wk_custom_product_type_data[DESCR_SL]:$wk_custom_product_type_data[CART_LANGUAGE];
        Registry::get('view')->assign('wk_custom_product_data', $wk_custom_product_data);
    }

} else {
    if ($_REQUEST['product_id']) {
        $product_data = fn_get_product_data($_REQUEST['product_id'], $auth, DESCR_SL, '', true, true, true, true, true, false, false);
     
        if (empty($product_data) || $product_data['company_id'] != $auth['company_id']) {
            fn_set_notification('E', __('warning'), __('incorrect_data_entered'));
            return array(CONTROLLER_STATUS_REDIRECT,'wk_custom_product.add');
        }
        if(!isset($_REQUEST['items_per_page']))
            $_REQUEST['items_per_page'] = 10;
        
        $_REQUEST['wk_parent_features'] = true;
        list($product_features, $features_search) = fn_get_paginated_product_features(
            $_REQUEST,
            $auth, $product_data,
            DESCR_SL
        );
        Tygh::$app['view']->assign('product_features', $product_features);
        Tygh::$app['view']->assign('features_search', $features_search);
        Tygh::$app['view']->assign('wk_product_data', $product_data);
        Tygh::$app['view']->assign('product_id', $_REQUEST['product_id']);
        $wk_custom_product_type_data = Fn_Get_Custom_Product_Type_data();
        if ($wk_custom_product_type_data) {
            Registry::get('view')->assign('wk_pricing_data', $product_data['custom_pricing_data']);
            $wk_custom_product_data = isset($wk_custom_product_type_data[DESCR_SL])?$wk_custom_product_type_data[DESCR_SL]:$wk_custom_product_type_data[CART_LANGUAGE];
            Registry::get('view')->assign('wk_custom_product_data', $wk_custom_product_data);
        }
        Registry::get('view')->assign('wk_selected_custom_product_data', $product_data['custom_product_data']);
        list($price,$list_price) = Fn_Wk_Calculate_Selected_Plan_price($product_data['custom_product_data'],  $product_data['custom_pricing_data'], $product_data);
        Registry::get('view')->assign('wk_plan_price', $price);
        Registry::get('view')->assign('wk_plan_list_price', $list_price);
    }
}

// i added get features code in there

$items_per_page = 100;
$path = $product_features = $features_search = array();
$id= $_REQUEST['result_ids'];
$category_ids = fn_set_session_data('category_ids', $category_id);
$_REQUEST['wk_product_data'] = $category_ids;
$category_id = $_REQUEST['category_id'];
if ($category_id) {
    $_params = array(
        'category_ids' => array($category_id),
        'product_id' => $_REQUEST['product_id'],
        'statuses' => array('A', 'H'),
        'exclude_group'=>true,
        'variants' => true,
        'plain' => false,
        'display_on' => '',
        'existent_only' => false,
        'variants_selected_only' => false
    );
    $_params = fn_array_merge($_REQUEST, $_params);
    list($product_features, $features_search) = fn_get_product_features($_params, $items_per_page, DESCR_SL);
    // fn_print_r($product_features);
}
Tygh::$app['view']->assign('product_id', $_REQUEST['product_id']);
Tygh::$app['view']->assign('category_id', $category_id);
Tygh::$app['view']->assign('product_features', $product_features);
Tygh::$app['view']->assign('features_search', $features_search);

if (!empty($_REQUEST['data_name'])) {
    Tygh::$app['view']->assign('data_name', $_REQUEST['data_name']);
}

}

if ($mode == ‘manage’) {
$auth = Tygh::$app[‘session’][‘auth’];
if (empty($auth[‘user_id’])) {
fn_set_notification(“W”, __(“warning”), (“wk_login_to_access_it”));
return array(CONTROLLER_STATUS_REDIRECT, ‘auth.login_form?return_url=’ . urlencode(Registry::get(‘config.current_url’)));
}
fn_add_breadcrumb(
(“wk_my_posts_breadcrumb”));

unset(Tygh::$app['session']['product_ids']);
unset(Tygh::$app['session']['selected_fields']);

$params = $_REQUEST;
$params['only_short_fields'] = true;
$params['apply_disabled_filters'] = true;
$params['extend'][] = 'companies';
$params['area'] = 'A';
$params['company_id'] = $auth['company_id'];
$params['wk_custom_product'] = true;
list($products, $search) = fn_get_products($params, Registry::get('settings.Appearance.admin_elements_per_page'), DESCR_SL);
fn_gather_additional_products_data($products, array('get_icon' => true, 'get_detailed' => true, 'get_options' => false, 'get_discounts' => false));

$page = $search['page'];
$valid_page = db_get_valid_page($page, $search['items_per_page'], $search['total_items']);
if ($page > $valid_page) {
    $_REQUEST['page'] = $valid_page;
    return array(CONTROLLER_STATUS_REDIRECT, Registry::get('config.current_url'));
}

Tygh::$app['view']->assign('products', $products);
Tygh::$app['view']->assign('search', $search);

}

if ($mode == ‘get_category_features’) {
$items_per_page = 100;
$path = $product_features = $features_search = array();
$id= $_REQUEST[‘result_ids’];
$category_id = $_REQUEST[‘category_id’];
if ($category_id) {
$_params = array(
‘category_ids’ => array($category_id),
‘product_id’ => $_REQUEST[‘product_id’],
‘statuses’ => array(‘A’, ‘H’),
‘exclude_group’=>true,
‘variants’ => true,
‘plain’ => false,
‘display_on’ => ‘’,
‘existent_only’ => false,
‘variants_selected_only’ => false
);
$_params = fn_array_merge($_REQUEST, $_params);
list($product_features, $features_search) = fn_get_product_features($_params, $items_per_page, DESCR_SL);
// fn_print_r($product_features);
}
Tygh::$app[‘view’]->assign(‘product_id’, $_REQUEST[‘product_id’]);
Tygh::$app[‘view’]->assign(‘category_id’, $category_id);
Tygh::$app[‘view’]->assign(‘product_features’, $product_features);
Tygh::$app[‘view’]->assign(‘features_search’, $features_search);
//Registry::get(‘view’)->assign(‘id’, $_REQUEST[‘data_name’]);

if (!empty($_REQUEST['data_name'])) {
    Tygh::$app['view']->assign('data_name', $_REQUEST['data_name']);
}
$res = Registry::get('view')->fetch('addons/wk_custom_product/views/wk_custom_product/components/products_update_features.tpl');
Registry::get('ajax')->assignHtml("$id", $res);
Registry::get('ajax')->assign('category_path', fn_get_category_path($category_id));
exit;

}

if ($mode == ‘check_expiry’) {
Fn_Check_Custom_Product_expiry();
}


fn_print_die($_REQUEST['product_data']);

old tpl code:

{* This file is not supported but exsists for backwards compatibility. Please don't use it. *}

{script src=“js/tygh/node_cloning.js”}
{assign var=“categories_company_id” value=$wk_product_data.company_id}
{if $wk_product_data.product_id}
{assign var=“id” value=$wk_product_data.product_id}
{assign var=“product_id” value=$wk_product_data.product_id}
{else}
{assign var=“id” value=0}
{assign var=“product_id” value=0}
{/if}
{include file=“addons/wk_custom_product/pickers/map.tpl”}
{assign var=“can_add_extra_products” value=false}
{if !$wk_product_data_list || count($wk_product_data_list)<10}
{assign var=“can_add_extra_products” value=true}
{/if}

{* product update form *} {if $can_add_extra_products} {math equation="x+1" x=$_key|default:0 assign="new_key"}
{include file="common/subheader.tpl" title=__("information")}
{__("name")}:
{math equation="rand()" assign="rnd"} {__("category")}: {$c_id = "0"} {include file="addons/wk_custom_product/views/wk_custom_product/components/category_picker.tpl" data_id="elm_categories" input_name="wk_product_data[{$new_key}][category_ids]" item_ids=$c_id hide_link=true hide_delete_button=false default_name='' display_input_id="wk_selected_category_input_field`$key`"}
{__("location")}: {include file="buttons/button.tpl" but_text=__("wk_select_location") but_role="action" but_icon="fa fa-map-marker" but_meta="ty-btn__secondary cm-wcp-map-dialog"}
{__("description")}: 4000 {__("wk_characters_left")}
{include file="addons/wk_custom_product/views/wk_custom_product/components/image_uploader.tpl" data_input_name="wk_product_data[`$new_key`][images][]"}

{*


{__(“youtube_embed”)}:


*}

			{include file="addons/wk_custom_product/views/wk_custom_product/components/custom_product_plan.tpl" show_input=true data_input_name="wk_product_data[`$new_key`]" show_total=true id=$new_key}
		  
		
{/if}
{include file="buttons/button.tpl" but_role="submit" but_name="dispatch[wk_custom_product.update]" but_text=__("wk_post_ad") but_meta="ty-btn__primary add-post-btn"}
{capture name="mainbox_title"}{__("wk_post_an_ad")}{/capture}

new tpl code:

                {*
				
{math equation="rand()" assign="rnd"} {__("category")}: {$c_id = "0"} {include file="addons/wk_custom_product/views/wk_custom_product/components/category_picker.tpl" data_id="elm_categories" input_name="wk_product_data[{$new_key}][category_ids]" item_ids=$c_id hide_link=true hide_delete_button=false default_name='' display_input_id="wk_selected_category_input_field`$key`"}
*}
{include file = "addons/wk_custom_product/views/wk_custom_product/components/products_update_features.tpl"}