Skip to content
Advertisement

CS-CART How to Create new Categories though API

I would like to create new categories in CS-Cart through its API.

So far I have this code (running it through browser, just for testing):

$cfg = get_config(); //connection to DB

$product_data = array();
$product_data["category"] = "Category Test API";
$product_data["company_id"] = 1;
$product_data["status"] = "A";


//CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_USERPWD, 'USER EMAIL'.":".'YOUR API:KEY');
curl_setopt($ch, CURLOPT_URL, $cfg["cscart_store_url"]."api/categories/");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json'
    )
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($product_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);

if( !curl_error($ch) ) {
    echo "no error";
    curl_close($ch);
    return json_decode($server_output, TRUE);
}
else {
    echo "error found!!!";
    print_r("Error: ".curl_error($ch));
    return 0;
}

There is a documentation here: https://docs.cs-cart.com/latest/developer_guide/api/entities/categories.html.

But I still couldn’t make it work although I did not get any errors from curl_exec.

Advertisement

Answer

As long as I understood, cs-cart API has some required values in order to make an INSERT API call. Thus, the categories should be already be created inside cs-cart dashboard and match them somehow in the PHP script

On the other hand, if you want to UPDAtE a product for example, you could UPDAte only the specified values you send to API call, without the need of sending all the required API values.

Last but not least, there are more than one Authorizations methods to make the call. I used CURL in order to do the API call and there is a parameter CURLOPT_HTTPHEADER which I have added the below code:

...
CURLOPT_HTTPHEADER => array(
    "Authorization: Basic XXXxxxxXXXXxxxXXXlzLmNvbS5ncjpKejMXXXxxxxXN0Y2MDQxenprbEXXXxxXXXE3Mg==",
    "Content-Type: application/json",
    "cache-control: no-cache"
),
...

Where Authorization is: Basic base64(user@example.com:API_KEY_taken_from_cscart)

Applying all the above the code and the API worked!

thanks everyone for the help and comments

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement