Code Projects
Api Connection
The first part of the code is about a connection with an API providing some data about products and adding them to the database by establishing a database connection to the shop’s website I’ve made this sample in about two days I was a Web Developer at cactusWeb when I was making that code the challenge I ran was that I remembered to unset the arrays by the end of the loop and I try to debug this issue finally I resolve it by unsetting the arrays at the end of the loop.
the code part
'johnhellas.oncloud.gr/S1Services/',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 1,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"service": "login",
"username": "",
"password":"",
"appId": "10001",
"COMPANY": "1000",
"BRANCH": "1000",
"MODULE": "0",
"REFID": "264"
} ',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
) ,
));
$response = curl_exec($curl);
// Then, after your curl_exec call:
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($curl);
$body = utf8_encode($body);
//
//print_r($header);
//print_r($response);
$arr_resp = json_decode($body, true);
//echo 'Last error: ', json_last_error_msg(), PHP_EOL, PHP_EOL;
echo $arr_resp['companyinfo'];
$text_input = $arr_resp['clientID'];
$text_input = iconv($text_input, 'UTF-16LE', 'UTF-8');
var_dump($text_input);
echo $text_input;
//echo utf8_decode($arr_resp['companyinfo']);
//var_dump($arr_resp);
//print_r($arr_resp);
$servername = "localhost:3306";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
echo "Initial character set is: " . $conn->character_set_name();
// Change character set to utf8
$conn->set_charset("utf8");
echo "Current character set is: " . $conn->character_set_name();
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
else
{
echo "The current character set is: $charset\n";
echo "connected";
}
/*last time called the erp service*/
$sql_last_call_timestamp = "SELECT date_time FROM erp_log ORDER BY ID DESC LIMIT 1";
$result_call_timestamp = $conn->query($sql_last_call_timestamp);
if ($result_call_timestamp->num_rows > 0)
{
// output data of each row
while ($row = $result_call_timestamp->fetch_assoc())
{
echo "datetime: " . $row["date_time"] . "
";
$last_import_date = $row["date_time"];
}
}
else
{
echo "0 results";
}
$conn->close();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'johnhellas.oncloud.gr/S1Services/JS/VitaNET_sync.endpoints/Products',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"clientID": "' . $arr_resp['clientID'] . '",
"appID": "10001",
"from": "2022/05/17",
"page": 0,
"pageSize": 2
}',
/*posa products fernei sto page size apo panw*/
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json charset=UTF-8'
) ,
));
$response = curl_exec($curl);
curl_close($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$body_product = substr($response, $header_size);
$body_product = iconv('Windows-1253', "UTF-8//TRANSLIT//IGNORE", $body_product);
$arr_resp_products = json_decode($body_product, true);
/*show products*/
echo "" . print_r($arr_resp_products, true) . "
";
echo "
respond";
$count_erp = $arr_resp_products[count] - 1;
echo "The COUNT IS :" . $count_erp;
$product_count = 0;
$updated_count = 0;
$created_count = 0;
/*the loop according to count by the ERP*/
while ($product_count <= 1)
{
/*array info*/
echo "NOW IS :" . $product_count;
$MTRL = ($arr_resp_products[data][$product_count][MTRL]);
$product_code = ($arr_resp_products[data][$product_count][PRODUCT_CODE]);
$product_barcode = ($arr_resp_products[data][$product_count][PRODUCT_BARCODE]);
$product_toweb = ($arr_resp_products[data][$product_count][TOWEB]);
$product_name = ($arr_resp_products[data][$product_count][NAME]);
$product_name_eng = ($arr_resp_products[data][$product_count][NAME_ENG]);
$product_description = ($arr_resp_products[data][$product_count][DESCRIPTION]);
$product_description_eng = ($arr_resp_products[data][$product_count][CCCENGDESC_ENG]);
$product_cat_id = ($arr_resp_products[data][$product_count][CATEGORY_ID]);
$product_country_id = ($arr_resp_products[data][$product_count][COUNTRY_ID]);
$product_country_name = ($arr_resp_products[data][$product_count][COUNTRY_NAME]);
$new_product = ($arr_resp_products[data][$product_count][NEWPRODUCT]);
$heroe = ($arr_resp_products[data][$product_count][HEROE]);
$brand_id = ($arr_resp_products[data][$product_count][BRAND_ID]);
$brand_code = ($arr_resp_products[data][$product_count][BRAND_CODE]);
$brand_name = ($arr_resp_products[data][$product_count][BRAND_NAME]);
$sex = ($arr_resp_products[data][$product_count][SEX]);
$age_id = ($arr_resp_products[data][$product_count][AGE_ID]);
$age_code = ($arr_resp_products[data][$product_count][AGE_CODE]);
$age_desciption = ($arr_resp_products[data][$product_count][AGE_DESCRIPTION]);
$product_dimensions = ($arr_resp_products[data][$product_count][PRODUCT_DIMENSIONS]);
$package_dimensions = ($arr_resp_products[data][$product_count][PACKAGE_DIMENSIONS]);
$package_num = ($arr_resp_products[data][$product_count][PACKAGE]);
$contents = ($arr_resp_products[data][$product_count][CONTENTS]);
$contents_eng = ($arr_resp_products[data][$product_count][CONTENTSENG]);
$puzzle_pieces = ($arr_resp_products[data][$product_count][PUZZLE_PIECES]);
$num_players = ($arr_resp_products[data][$product_count][NUM_OF_PLAYERS]);
$gameduration = ($arr_resp_products[data][$product_count][GAME_DURATION]);
$volume = ($arr_resp_products[data][$product_count][VOLUME]);
$weight = ($arr_resp_products[data][$product_count][WEIGHT]);
$odigies = ($arr_resp_products[data][$product_count][ODIGIES]);
$youtube = ($arr_resp_products[data][$product_count][YOUTUBE]);
$QTY = ($arr_resp_products[data][$product_count][QTY]);
$price = ($arr_resp_products[data][$product_count][PRICE]);
$servername = "localhost:3306";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$charset = mysql_client_encoding($conn);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
else
{
echo "The current character set is: $charset\n";
echo "connected";
}
/****************************************is the prod already in store?*****************************************/
$sql_product_id_finder = "SELECT * FROM oc_product WHERE model={$product_barcode} ";
if ($result = $conn->query($sql_product_id_finder))
{
while ($row_finder = $result->fetch_assoc())
{
$data_finder[] = $row_finder;
}
/*this is the product id for the current product*/
$checked_product_id = $data_finder[0][product_id];
echo "BARCODE" . $product_barcode;
echo "
";
echo $checked_product_id;
echo "
";
}
if ($checked_product_id)
{
/******************************************** UPDATE *********************************/
echo "found the prod";
$sql = "UPDATE oc_product SET sku = '{$product_code}' ,weight='{$weight}',is_new='{$new_product}',price='{$price}',country='{$product_country_name}',package_size='{$package_dimensions}',product_size='{$product_dimensions}',volume='{$volume}',package='{$package_num}',status='{$product_toweb}',manufacturer_id='{$brand_id}',youtube='{$youtube}',tax_class_id='9',quantity='10000',stock_status_id='6' WHERE model='{$product_barcode}'
";
if ($conn->query($sql) === true)
{
echo "New record in oc.product UPDATED successfully";
}
else
{
echo "Error: " . $sql . "
" . $conn->error;
}
/*working in table oc_product_description greek */
$sql_insertion_product_description_gr = "UPDATE oc_product_description SET name='{$product_name}',description='{$product_description}',content='{$contents}',meta_title='{$product_name}',meta_description='{$product_description}' WHERE product_id='{$checked_product_id}' AND language_id='3'";
if ($conn->query($sql_insertion_product_description_gr) === true)
{
echo "New record in oc_product_description UPDATED successfully";
}
else
{
echo "Error oc_product_description: " . $sql_insertion_product_description_gr . "
" . $conn->error;
}
/*working in table oc_product_description english */
$product_name_eng = str_replace("'", "", $product_name_eng);
$product_description_eng = str_replace("'", "", $product_description_eng);
$sql_insertion_product_description_en = "UPDATE oc_product_description SET name='{$product_name_eng}',description='{$product_description_eng}',content='{$contents_eng}',meta_title='{$product_name_eng}',meta_description='$product_description_eng' WHERE product_id='{$checked_product_id}' AND language_id='1'";
$conn->query($sql_insertion_product_description_en);
if ($conn->query($sql_insertion_product_description_en) === true)
{
echo "New record in oc_product_description ENG UPDATED successfully";
}
else
{
echo "Error oc_product_description ENG: " . $sql_insertion_product_description_en . "
" . $conn->error;
}
/*check if product id exists in table oc_product_to_download */
$sql_oc_product_to_download = "SELECT * FROM oc_product_to_download WHERE product_id={$checked_product_id}";
if ($result = $conn->query($sql_oc_product_to_download))
{
while ($row_oc_product_to_download = $result->fetch_assoc())
{
$data_oc_product_to_download[] = $row_oc_product_to_download;
}
$product_to_down = $data_oc_product_to_download[0][product_id];
if ($product_to_down)
{
$sql_delete_product_to_download = "DELETE FROM oc_product_to_download WHERE product_id = {$checked_product_id} ";
$sql_insertion_product_to_download = "INSERT INTO oc_product_to_download (product_id,download_id) VALUES ('{$latest_product_id}','{$odigies}') ";
$conn->query($sql_delete_product_to_download);
$conn->query($sql_insertion_product_to_download);
}
else if ($odigies)
{
$sql_insertion_product_to_download = "INSERT INTO oc_product_to_download (product_id,download_id) VALUES ('{$latest_product_id}','{$odigies}') ";
/*this fires the addition*/
$conn->query($sql_insertion_product_to_download);
}
/*deletion of existing attributes*/
$sql_delete_product_to_attribute = "DELETE FROM oc_product_attribute WHERE product_id = {$checked_product_id} ";
$conn->query($sql_delete_product_to_attribute);
if ($sex)
{
if ($sex == "1" || $sex == "3")
{
$gen = 'Αγόρι';
$sql_insertion_gender_attibute_boy = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','1','3','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_boy);
$gen = 'Boy';
$sql_insertion_gender_attibute_en_boy = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','1','1','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_en_boy);
}
if ($sex == 2 || $sex == 3)
{
$gen = 'Κορίτσι';
$sql_insertion_gender_attibute_girl = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','1','3','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_girl);
$gen = 'Girl';
$sql_insertion_gender_attibute_girl_en = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','1','1','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_girl_en);
}
}
if ($heroe)
{
$sql_insertion_iroes = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','2','3','{$heroe}') ";
$conn->query($sql_insertion_iroes);
$sql_insertion_attibute_en_iroes = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','2','1','{$heroe}') ";
$conn->query($sql_insertion_attibute_en_iroes);
}
if ($puzzle_pieces && $puzzle_pieces != "0")
{
$sql_insertion_puzzle_pieces = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','3','3','{$puzzle_pieces}') ";
$conn->query($sql_insertion_puzzle_pieces);
$sql_insertion_attibute_en_puzzle_pieces = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','3','1','{$puzzle_pieces}') ";
$conn->query($sql_insertion_attibute_en_puzzle_pieces);
}
/*add num players attribute the attr id is 4*/
if ($num_players && $num_players != "0")
{
$sql_insertion_num_players = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','4','3','{$num_players}') ";
$conn->query($sql_insertion_num_players);
$sql_insertion_attibute_en_num_players = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','4','1','{$num_players}') ";
$conn->query($sql_insertion_attibute_en_num_players);
}
/*add num players attribute the attr id is 5*/
if ($gameduration && $gameduration != "0")
{
$sql_insertion_gameduration = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','5','3','{$gameduration}') ";
$conn->query($sql_insertion_gameduration);
$sql_insertion_attibute_en_gameduration = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','5','1','{$gameduration}') ";
$conn->query($sql_insertion_attibute_en_gameduration);
}
/*manipulation of age*/
$fix_age_range = str_replace("+", "", $age_desciption);
if ($fix_age_range == '1,5' || $fix_age_range == '1.5' || $fix_age_range == '1 1/2')
{
$fix_age_range = 1.5;
}
elseif ($fix_age_range == '2,5' || $fix_age_range == '2.5' || $fix_age_range == '2 1/2')
{
$fix_age_range = 2.5;
}
elseif ($fix_age_range == '' || $fix_age_range == ' ')
{
$fix_age_range = 99;
}
/*gets the product options from the DB*/
$sql_product_option = "SELECT option_value_id,name FROM oc_option_value_description
WHERE name LIKE '%years%'
ORDER BY option_value_id";
if ($result_2 = $conn->query($sql_product_option))
{
while ($row_2 = $result_2->fetch_assoc())
{
$data_2[] = $row_2;
echo "";
/* print_r($data_2);*/
echo "
";
}
/*looping from the DB results and matching*/
foreach ($data_2 as $age_rec)
{
$age_name = explode(' ', $age_rec['name']);
$age_name = explode('-', $age_name[0]);
if ($age_name[0] >= $fix_age_range)
$option_value_ids[] = $age_rec['option_value_id'];
}
}
/*@$option_value_ids is the found ids */
print_r($option_value_ids);
if ($fix_age_range == "99")
{
$fix_age_range = "99";
}
else
{
$fix_age_range = $fix_age_range . "-99";
}
/*deletion of age attribute*/
$sql_deletion_attr_age = "DELETE FROM oc_product_option WHERE product_id = '{$checked_product_id}'AND option_id='1' ";
$conn->query($sql_deletion_attr_age);
$sql_insertion_attr_age = "INSERT INTO oc_product_option (product_id,option_id,value,required) VALUES ('{$checked_product_id}','1','{$fix_age_range}','1') ";
$conn->query($sql_insertion_attr_age);
$sql_product_option_get_id = "SELECT product_option_id FROM oc_product_option
WHERE product_id = '{$checked_product_id}'";
if ($result_3 = $conn->query($sql_product_option_get_id))
{
while ($row_3 = $result_3->fetch_assoc())
{
$data_3[] = $row_3;
/* echo "";
print_r($data_3);
echo "
";*/
}
}
$latest_prod_option = $data_3[0][product_option_id];
/*echo "test".$fix_age_range;*/
$sql_deletion_opt_age = "DELETE FROM oc_product_option_value WHERE product_id = '{$checked_product_id}'AND option_id='1' ";
$conn->query($sql_deletion_opt_age);
foreach ($option_value_ids as $option_value_id)
{
/* print_r ($option_value_id);*/
$sql_insertion_option_value = "INSERT INTO oc_product_option_value (product_option_id,product_id,option_id,option_value_id,quantity,subtract,price) VALUES ('$latest_prod_option','$checked_product_id','1','$option_value_id','0','0','0') ";
$conn->query($sql_insertion_option_value);
}
}
$sql_deletion_product_to_category = "DELETE FROM oc_product_to_category WHERE product_id='{$checked_product_id}'";
$conn->query($sql_deletion_product_to_category);
/*working in table oc_product_to_category */
$sql_insertion_product_to_category = "INSERT INTO oc_product_to_category (category_id,product_id) VALUES ('{$product_cat_id}','{$checked_product_id}') ";
$conn->query($sql_insertion_product_to_category);
$updated_count++;
}
/********************************************END OF UPDATE*************************************/
else
{
$sql = "INSERT INTO oc_product (model,sku,weight,is_new,price,country,package_size,product_size,volume,package,status,youtube,manufacturer_id,tax_class_id,quantity,stock_status_id)
VALUES ('{$product_barcode}','{$product_code}','{$weight}','{$new_product}','{$price}','{$product_country_name}','{$package_dimensions}','{$product_dimensions}','{$volume}','{$package_num}','{$product_toweb}','{$youtube}',{$brand_id},'9','10000','6')";
if ($conn->query($sql) === true)
{
echo "New record in oc.product created successfully";
}
else
{
echo "Error: " . $sql . "
" . $conn->error;
}
/*******get the generated product id******/
$sql_product_id_create = "SELECT * FROM oc_product WHERE model=$product_barcode ";
if ($result = $conn->query($sql_product_id_create))
{
while ($row = $result->fetch_assoc())
{
$data[] = $row;
}
/*this is the product id for the current product*/
$latest_product_id = $data[0][product_id];
echo $latest_product_id;
echo "
";
/*working in table oc_product_description greek */
$sql_insertion_product_description_gr = "UPDATE oc_product_description SET language_id='3',name='{$product_name}',description='{$product_description}',content='{$contents}',meta_title='{$product_name}',meta_description='{$product_description}' WHERE product_id='{$latest_product_id}'";
$conn->query($sql_insertion_product_description_gr);
if ($conn->query($sql_insertion_product_description_gr) === true)
{
echo "CREATE:New record in oc_product_description UPDATED successfully";
}
else
{
echo "Error oc_product_description: " . $sql . "
" . $conn->error;
}
/*working in table oc_product_description english */
$product_name_eng = str_replace("'", "", $product_name_eng);
$product_description_eng = str_replace("'", "", $product_description_eng);
$sql_insertion_product_description_en = "UPDATE oc_product_description SET language_id='3',name='{$product_name_eng}',description='{$product_description_eng}',content='{$contents_eng}',meta_title='{$product_name_eng}',meta_description='{$product_description_eng}' WHERE product_id='{$latest_product_id}'";
$conn->query($sql_insertion_product_description_en);
if ($conn->query($sql_insertion_product_description_en) === true)
{
echo "CREATE:New record in oc_product_description ENG UPDATED successfully";
}
else
{
echo "Error oc_product_description ENG: " . $sql . "
" . $conn->error;
}
}
/*check if product id exists in table oc_product_to_download */
$sql_oc_product_to_download = "SELECT * FROM oc_product_to_download WHERE product_id=$latest_product_id";
if ($result = $conn->query($sql_oc_product_to_download))
{
while ($row_oc_product_to_download = $result->fetch_assoc())
{
$data_oc_product_to_download[] = $row_oc_product_to_download;
}
$product_to_down = $data_oc_product_to_download[0][product_id];
if ($product_to_down)
{
$sql_delete_product_to_download = "DELETE FROM oc_product_to_download WHERE product_id = {$latest_product_id} ";
$sql_insertion_product_to_download = "INSERT INTO oc_product_to_download (product_id,download_id) VALUES ('{$latest_product_id}','{$odigies}') ";
$conn->query($sql_delete_product_to_download);
$conn->query($sql_insertion_product_to_download);
}
else if ($odigies)
{
$sql_insertion_product_to_download = "INSERT INTO oc_product_to_download (product_id,download_id) VALUES ('{$latest_product_id}','{$odigies}') ";
/*this fires the addition*/
$conn->query($sql_insertion_product_to_download);
}
}
/*working in table oc_product_to_store */
$sql_insertion_product_to_store = "INSERT INTO oc_product_to_store (store_id,product_id) VALUES ('0','{$latest_product_id}') ";
$conn->query($sql_insertion_product_to_store);
/*working in table oc_product_description greek */
$sql_insertion_product_description_gr = "INSERT INTO oc_product_description (product_id,language_id,name,description,content,meta_title,meta_description) VALUES ('$latest_product_id','3','{$product_name}','{$product_description}','{$contents}','{$product_name}','{$product_description}') ";
$conn->query($sql_insertion_product_description_gr);
/*working in table oc_product_description english */
$product_name_eng = str_replace("'", "", $product_name_eng);
$product_description_eng = str_replace("'", "", $product_description_eng);
$sql_insertion_product_description_en = "INSERT INTO oc_product_description (product_id,language_id,name,description,content,meta_title,meta_description) VALUES ('$latest_product_id','1','{$product_name_eng}','{$product_description_eng}','{$contents_eng}','{$product_name_eng}','{$product_description_eng}') ";
$conn->query($sql_insertion_product_description_en);
/*product attributes set up*/
/*gender check the product attr 1 is gender greek is lang id 3 eng is lang id 1 */
/*no worries you can sideload two values*/
if ($sex)
{
if ($sex == "1" || $sex == "3")
{
$gen = 'Αγόρι';
$sql_insertion_gender_attibute_boy = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','1','3','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_boy);
$gen = 'Boy';
$sql_insertion_gender_attibute_en_boy = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','1','1','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_en_boy);
}
if ($sex == 2 || $sex == 3)
{
$gen = 'Κορίτσι';
$sql_insertion_gender_attibute_girl = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','1','3','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_girl);
$gen = 'Girl';
$sql_insertion_gender_attibute_girl_en = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','1','1','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_girl_en);
}
/*iroas add*/
if ($heroe)
{
$sql_insertion_iroes = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','2','3','{$heroe}') ";
$conn->query($sql_insertion_iroes);
$sql_insertion_attibute_en_iroes = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','2','1','{$heroe}') ";
$conn->query($sql_insertion_attibute_en_iroes);
}
if ($puzzle_pieces && $puzzle_pieces != "1")
{
$sql_insertion_puzzle_pieces = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','3','3','{$puzzle_pieces}') ";
$conn->query($sql_insertion_puzzle_pieces);
$sql_insertion_attibute_puzzle_pieces = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','3','1','{$puzzle_pieces}') ";
$conn->query($sql_insertion_attibute_puzzle_pieces);
}
/*add puzzle pieces attribute the attr id is 3*/
if ($puzzle_pieces && $puzzle_pieces != "0")
{
$sql_insertion_puzzle_pieces = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','3','3','{$puzzle_pieces}') ";
$conn->query($sql_insertion_puzzle_pieces);
$sql_insertion_attibute_en_puzzle_pieces = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','3','1','{$puzzle_pieces}') ";
$conn->query($sql_insertion_attibute_en_puzzle_pieces);
}
/*add num players attribute the attr id is 4*/
if ($num_players && $num_players != "0")
{
$sql_insertion_num_players = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','4','3','{$num_players}') ";
$conn->query($sql_insertion_num_players);
$sql_insertion_attibute_en_num_players = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','4','1','{$num_players}') ";
$conn->query($sql_insertion_attibute_en_num_players);
}
/*add num players attribute the attr id is 5*/
if ($gameduration && $gameduration != "0")
{
$sql_insertion_gameduration = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','5','3','{$gameduration}') ";
$conn->query($sql_insertion_gameduration);
$sql_insertion_attibute_en_gameduration = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','5','1','{$gameduration}') ";
$conn->query($sql_insertion_attibute_en_gameduration);
}
}
/*manipulation of age*/
$fix_age_range = str_replace("+", "", $age_desciption);
if ($fix_age_range == '1,5' || $fix_age_range == '1.5' || $fix_age_range == '1 1/2')
{
$fix_age_range = 1.5;
}
elseif ($fix_age_range == '2,5' || $fix_age_range == '2.5' || $fix_age_range == '2 1/2')
{
$fix_age_range = 2.5;
}
elseif ($fix_age_range == '' || $fix_age_range == ' ')
{
$fix_age_range = 99;
}
/*gets the product options from the DB*/
$sql_product_option = "SELECT option_value_id,name FROM oc_option_value_description
WHERE name LIKE '%years%'
ORDER BY option_value_id";
if ($result_2 = $conn->query($sql_product_option))
{
while ($row_2 = $result_2->fetch_assoc())
{
$data_2[] = $row_2;
echo "";
/* print_r($data_2);*/
echo "
";
}
/*looping from the DB results and matching*/
foreach ($data_2 as $age_rec)
{
$age_name = explode(' ', $age_rec['name']);
$age_name = explode('-', $age_name[0]);
if ($age_name[0] >= $fix_age_range)
$option_value_ids[] = $age_rec['option_value_id'];
}
}
/*@$option_value_ids is the found ids */
print_r($option_value_ids);
if ($fix_age_range == "99")
{
$fix_age_range = "99";
}
else
{
$fix_age_range = $fix_age_range . "-99";
}
$sql_insertion_attr_age = "INSERT INTO oc_product_option (product_id,option_id,value,required) VALUES ('{$latest_product_id}','1','{$fix_age_range}','1') ";
$conn->query($sql_insertion_attr_age);
$sql_product_option_get_id = "SELECT product_option_id FROM oc_product_option
WHERE product_id = '{$latest_product_id}'";
if ($result_3 = $conn->query($sql_product_option_get_id))
{
while ($row_3 = $result_3->fetch_assoc())
{
$data_3[] = $row_3;
/* echo "";
print_r($data_3);
echo "
";*/
}
}
$latest_prod_option = $data_3[0][product_option_id];
/*echo "test".$fix_age_range;*/
$created_count++;
foreach ($option_value_ids as $option_value_id)
{
/* print_r ($option_value_id);*/
$sql_insertion_option_value = "INSERT INTO oc_product_option_value (product_option_id,product_id,option_id,option_value_id,quantity,subtract,price) VALUES ('$latest_prod_option','$latest_product_id','1','$option_value_id','0','0','0') ";
$conn->query($sql_insertion_option_value);
/*working in table oc_product_to_category */
$sql_insertion_product_to_category = "INSERT INTO oc_product_to_category (category_id,product_id) VALUES ('{$product_cat_id}','{$latest_product_id}') ";
$conn->query($sql_insertion_product_to_category);
}
/*get the categories from erp*/
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'johnhellas.oncloud.gr/S1Services/JS/VitaNET_sync.endpoints/Categories',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"clientID": "' . $arr_resp['clientID'] . '" ,
"appID": "10001"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
) ,
));
$response = curl_exec($curl);
curl_close($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$body_cats = substr($response, $header_size);
$body_cats = iconv('Windows-1253', "UTF-8//TRANSLIT//IGNORE", $body_cats);
echo "";
print_r($body_cats);
echo "
";
/*the product cat id is like the softone id until now*/
}
$product_count++;
$latest_product_id = "";
unset($data_finder);
unset($data);
unset($data_2);
unset($data_3);
unset($option_value_ids);
unset($age_name);
}
/*working in table oc_product_to_category */
$sql_insertion_log = "INSERT INTO erp_log (erp_created,erp_updated) VALUES ('{$created_count}','{$updated_count}') ";
$conn->query($sql_insertion_log);
SECOND EXAMPLE
Is about the bug I wrote before about the ivory search extension written in functions.php of the child theme below the code:
/*adds a meta in each product to help the search*/
add_action('save_post_product', 'mp_sync_on_product_save', 10, 3);
function mp_sync_on_product_save( $post_id, $post, $update ) {
$product = wc_get_product( $post_id );
$thetitle=$product->get_title();
$unwanted = array('ά'=>'α', 'έ'=>'ε', 'ή'=>'η', 'ί'=>'ι', 'ό'=>'ο', 'ύ'=>'υ', 'ώ'=>'ω');
$fixed_title_for_search= strtr( $thetitle, $unwanted);
$meta_key = '_searchcactus';
$meta_value = $fixed_title_for_search;
$unique = true;
add_post_meta( $post_id, $meta_key, $meta_value, $unique );
}
The first part of the code is about a connection with an API providing some data about products and adding them to the database by establishing a database connection to the shop's website I've made this sample in about two days I was a Web Developer at cactusWeb when I was making that code the challenge I ran was that I remembered to unset the arrays by the end of the loop and I try to debug this issue finally I resolve it by unsetting the arrays at the end of the loop.
Bellow the sample:
'johnhellas.oncloud.gr/S1Services/',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 1,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"service": "login",
"username": "",
"password":"",
"appId": "10001",
"COMPANY": "1000",
"BRANCH": "1000",
"MODULE": "0",
"REFID": "264"
} ',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
) ,
));
$response = curl_exec($curl);
// Then, after your curl_exec call:
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($curl);
$body = utf8_encode($body);
//
//print_r($header);
//print_r($response);
$arr_resp = json_decode($body, true);
//echo 'Last error: ', json_last_error_msg(), PHP_EOL, PHP_EOL;
echo $arr_resp['companyinfo'];
$text_input = $arr_resp['clientID'];
$text_input = iconv($text_input, 'UTF-16LE', 'UTF-8');
var_dump($text_input);
echo $text_input;
//echo utf8_decode($arr_resp['companyinfo']);
//var_dump($arr_resp);
//print_r($arr_resp);
$servername = "localhost:3306";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
echo "Initial character set is: " . $conn->character_set_name();
// Change character set to utf8
$conn->set_charset("utf8");
echo "Current character set is: " . $conn->character_set_name();
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
else
{
echo "The current character set is: $charset\n";
echo "connected";
}
/*last time called the erp service*/
$sql_last_call_timestamp = "SELECT date_time FROM erp_log ORDER BY ID DESC LIMIT 1";
$result_call_timestamp = $conn->query($sql_last_call_timestamp);
if ($result_call_timestamp->num_rows > 0)
{
// output data of each row
while ($row = $result_call_timestamp->fetch_assoc())
{
echo "datetime: " . $row["date_time"] . "
";
$last_import_date = $row["date_time"];
}
}
else
{
echo "0 results";
}
$conn->close();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'johnhellas.oncloud.gr/S1Services/JS/VitaNET_sync.endpoints/Products',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"clientID": "' . $arr_resp['clientID'] . '",
"appID": "10001",
"from": "2022/05/17",
"page": 0,
"pageSize": 2
}',
/*posa products fernei sto page size apo panw*/
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json charset=UTF-8'
) ,
));
$response = curl_exec($curl);
curl_close($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$body_product = substr($response, $header_size);
$body_product = iconv('Windows-1253', "UTF-8//TRANSLIT//IGNORE", $body_product);
$arr_resp_products = json_decode($body_product, true);
/*show products*/
echo "" . print_r($arr_resp_products, true) . "
";
echo "
respond";
$count_erp = $arr_resp_products[count] - 1;
echo "The COUNT IS :" . $count_erp;
$product_count = 0;
$updated_count = 0;
$created_count = 0;
/*the loop according to count by the ERP*/
while ($product_count <= 1)
{
/*array info*/
echo "NOW IS :" . $product_count;
$MTRL = ($arr_resp_products[data][$product_count][MTRL]);
$product_code = ($arr_resp_products[data][$product_count][PRODUCT_CODE]);
$product_barcode = ($arr_resp_products[data][$product_count][PRODUCT_BARCODE]);
$product_toweb = ($arr_resp_products[data][$product_count][TOWEB]);
$product_name = ($arr_resp_products[data][$product_count][NAME]);
$product_name_eng = ($arr_resp_products[data][$product_count][NAME_ENG]);
$product_description = ($arr_resp_products[data][$product_count][DESCRIPTION]);
$product_description_eng = ($arr_resp_products[data][$product_count][CCCENGDESC_ENG]);
$product_cat_id = ($arr_resp_products[data][$product_count][CATEGORY_ID]);
$product_country_id = ($arr_resp_products[data][$product_count][COUNTRY_ID]);
$product_country_name = ($arr_resp_products[data][$product_count][COUNTRY_NAME]);
$new_product = ($arr_resp_products[data][$product_count][NEWPRODUCT]);
$heroe = ($arr_resp_products[data][$product_count][HEROE]);
$brand_id = ($arr_resp_products[data][$product_count][BRAND_ID]);
$brand_code = ($arr_resp_products[data][$product_count][BRAND_CODE]);
$brand_name = ($arr_resp_products[data][$product_count][BRAND_NAME]);
$sex = ($arr_resp_products[data][$product_count][SEX]);
$age_id = ($arr_resp_products[data][$product_count][AGE_ID]);
$age_code = ($arr_resp_products[data][$product_count][AGE_CODE]);
$age_desciption = ($arr_resp_products[data][$product_count][AGE_DESCRIPTION]);
$product_dimensions = ($arr_resp_products[data][$product_count][PRODUCT_DIMENSIONS]);
$package_dimensions = ($arr_resp_products[data][$product_count][PACKAGE_DIMENSIONS]);
$package_num = ($arr_resp_products[data][$product_count][PACKAGE]);
$contents = ($arr_resp_products[data][$product_count][CONTENTS]);
$contents_eng = ($arr_resp_products[data][$product_count][CONTENTSENG]);
$puzzle_pieces = ($arr_resp_products[data][$product_count][PUZZLE_PIECES]);
$num_players = ($arr_resp_products[data][$product_count][NUM_OF_PLAYERS]);
$gameduration = ($arr_resp_products[data][$product_count][GAME_DURATION]);
$volume = ($arr_resp_products[data][$product_count][VOLUME]);
$weight = ($arr_resp_products[data][$product_count][WEIGHT]);
$odigies = ($arr_resp_products[data][$product_count][ODIGIES]);
$youtube = ($arr_resp_products[data][$product_count][YOUTUBE]);
$QTY = ($arr_resp_products[data][$product_count][QTY]);
$price = ($arr_resp_products[data][$product_count][PRICE]);
$servername = "localhost:3306";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$charset = mysql_client_encoding($conn);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
else
{
echo "The current character set is: $charset\n";
echo "connected";
}
/****************************************is the prod already in store?*****************************************/
$sql_product_id_finder = "SELECT * FROM oc_product WHERE model={$product_barcode} ";
if ($result = $conn->query($sql_product_id_finder))
{
while ($row_finder = $result->fetch_assoc())
{
$data_finder[] = $row_finder;
}
/*this is the product id for the current product*/
$checked_product_id = $data_finder[0][product_id];
echo "BARCODE" . $product_barcode;
echo "
";
echo $checked_product_id;
echo "
";
}
if ($checked_product_id)
{
/******************************************** UPDATE *********************************/
echo "found the prod";
$sql = "UPDATE oc_product SET sku = '{$product_code}' ,weight='{$weight}',is_new='{$new_product}',price='{$price}',country='{$product_country_name}',package_size='{$package_dimensions}',product_size='{$product_dimensions}',volume='{$volume}',package='{$package_num}',status='{$product_toweb}',manufacturer_id='{$brand_id}',youtube='{$youtube}',tax_class_id='9',quantity='10000',stock_status_id='6' WHERE model='{$product_barcode}'
";
if ($conn->query($sql) === true)
{
echo "New record in oc.product UPDATED successfully";
}
else
{
echo "Error: " . $sql . "
" . $conn->error;
}
/*working in table oc_product_description greek */
$sql_insertion_product_description_gr = "UPDATE oc_product_description SET name='{$product_name}',description='{$product_description}',content='{$contents}',meta_title='{$product_name}',meta_description='{$product_description}' WHERE product_id='{$checked_product_id}' AND language_id='3'";
if ($conn->query($sql_insertion_product_description_gr) === true)
{
echo "New record in oc_product_description UPDATED successfully";
}
else
{
echo "Error oc_product_description: " . $sql_insertion_product_description_gr . "
" . $conn->error;
}
/*working in table oc_product_description english */
$product_name_eng = str_replace("'", "", $product_name_eng);
$product_description_eng = str_replace("'", "", $product_description_eng);
$sql_insertion_product_description_en = "UPDATE oc_product_description SET name='{$product_name_eng}',description='{$product_description_eng}',content='{$contents_eng}',meta_title='{$product_name_eng}',meta_description='$product_description_eng' WHERE product_id='{$checked_product_id}' AND language_id='1'";
$conn->query($sql_insertion_product_description_en);
if ($conn->query($sql_insertion_product_description_en) === true)
{
echo "New record in oc_product_description ENG UPDATED successfully";
}
else
{
echo "Error oc_product_description ENG: " . $sql_insertion_product_description_en . "
" . $conn->error;
}
/*check if product id exists in table oc_product_to_download */
$sql_oc_product_to_download = "SELECT * FROM oc_product_to_download WHERE product_id={$checked_product_id}";
if ($result = $conn->query($sql_oc_product_to_download))
{
while ($row_oc_product_to_download = $result->fetch_assoc())
{
$data_oc_product_to_download[] = $row_oc_product_to_download;
}
$product_to_down = $data_oc_product_to_download[0][product_id];
if ($product_to_down)
{
$sql_delete_product_to_download = "DELETE FROM oc_product_to_download WHERE product_id = {$checked_product_id} ";
$sql_insertion_product_to_download = "INSERT INTO oc_product_to_download (product_id,download_id) VALUES ('{$latest_product_id}','{$odigies}') ";
$conn->query($sql_delete_product_to_download);
$conn->query($sql_insertion_product_to_download);
}
else if ($odigies)
{
$sql_insertion_product_to_download = "INSERT INTO oc_product_to_download (product_id,download_id) VALUES ('{$latest_product_id}','{$odigies}') ";
/*this fires the addition*/
$conn->query($sql_insertion_product_to_download);
}
/*deletion of existing attributes*/
$sql_delete_product_to_attribute = "DELETE FROM oc_product_attribute WHERE product_id = {$checked_product_id} ";
$conn->query($sql_delete_product_to_attribute);
if ($sex)
{
if ($sex == "1" || $sex == "3")
{
$gen = 'Αγόρι';
$sql_insertion_gender_attibute_boy = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','1','3','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_boy);
$gen = 'Boy';
$sql_insertion_gender_attibute_en_boy = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','1','1','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_en_boy);
}
if ($sex == 2 || $sex == 3)
{
$gen = 'Κορίτσι';
$sql_insertion_gender_attibute_girl = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','1','3','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_girl);
$gen = 'Girl';
$sql_insertion_gender_attibute_girl_en = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','1','1','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_girl_en);
}
}
if ($heroe)
{
$sql_insertion_iroes = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','2','3','{$heroe}') ";
$conn->query($sql_insertion_iroes);
$sql_insertion_attibute_en_iroes = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','2','1','{$heroe}') ";
$conn->query($sql_insertion_attibute_en_iroes);
}
if ($puzzle_pieces && $puzzle_pieces != "0")
{
$sql_insertion_puzzle_pieces = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','3','3','{$puzzle_pieces}') ";
$conn->query($sql_insertion_puzzle_pieces);
$sql_insertion_attibute_en_puzzle_pieces = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','3','1','{$puzzle_pieces}') ";
$conn->query($sql_insertion_attibute_en_puzzle_pieces);
}
/*add num players attribute the attr id is 4*/
if ($num_players && $num_players != "0")
{
$sql_insertion_num_players = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','4','3','{$num_players}') ";
$conn->query($sql_insertion_num_players);
$sql_insertion_attibute_en_num_players = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','4','1','{$num_players}') ";
$conn->query($sql_insertion_attibute_en_num_players);
}
/*add num players attribute the attr id is 5*/
if ($gameduration && $gameduration != "0")
{
$sql_insertion_gameduration = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','5','3','{$gameduration}') ";
$conn->query($sql_insertion_gameduration);
$sql_insertion_attibute_en_gameduration = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$checked_product_id}','5','1','{$gameduration}') ";
$conn->query($sql_insertion_attibute_en_gameduration);
}
/*manipulation of age*/
$fix_age_range = str_replace("+", "", $age_desciption);
if ($fix_age_range == '1,5' || $fix_age_range == '1.5' || $fix_age_range == '1 1/2')
{
$fix_age_range = 1.5;
}
elseif ($fix_age_range == '2,5' || $fix_age_range == '2.5' || $fix_age_range == '2 1/2')
{
$fix_age_range = 2.5;
}
elseif ($fix_age_range == '' || $fix_age_range == ' ')
{
$fix_age_range = 99;
}
/*gets the product options from the DB*/
$sql_product_option = "SELECT option_value_id,name FROM oc_option_value_description
WHERE name LIKE '%years%'
ORDER BY option_value_id";
if ($result_2 = $conn->query($sql_product_option))
{
while ($row_2 = $result_2->fetch_assoc())
{
$data_2[] = $row_2;
echo "";
/* print_r($data_2);*/
echo "
";
}
/*looping from the DB results and matching*/
foreach ($data_2 as $age_rec)
{
$age_name = explode(' ', $age_rec['name']);
$age_name = explode('-', $age_name[0]);
if ($age_name[0] >= $fix_age_range)
$option_value_ids[] = $age_rec['option_value_id'];
}
}
/*@$option_value_ids is the found ids */
print_r($option_value_ids);
if ($fix_age_range == "99")
{
$fix_age_range = "99";
}
else
{
$fix_age_range = $fix_age_range . "-99";
}
/*deletion of age attribute*/
$sql_deletion_attr_age = "DELETE FROM oc_product_option WHERE product_id = '{$checked_product_id}'AND option_id='1' ";
$conn->query($sql_deletion_attr_age);
$sql_insertion_attr_age = "INSERT INTO oc_product_option (product_id,option_id,value,required) VALUES ('{$checked_product_id}','1','{$fix_age_range}','1') ";
$conn->query($sql_insertion_attr_age);
$sql_product_option_get_id = "SELECT product_option_id FROM oc_product_option
WHERE product_id = '{$checked_product_id}'";
if ($result_3 = $conn->query($sql_product_option_get_id))
{
while ($row_3 = $result_3->fetch_assoc())
{
$data_3[] = $row_3;
/* echo "";
print_r($data_3);
echo "
";*/
}
}
$latest_prod_option = $data_3[0][product_option_id];
/*echo "test".$fix_age_range;*/
$sql_deletion_opt_age = "DELETE FROM oc_product_option_value WHERE product_id = '{$checked_product_id}'AND option_id='1' ";
$conn->query($sql_deletion_opt_age);
foreach ($option_value_ids as $option_value_id)
{
/* print_r ($option_value_id);*/
$sql_insertion_option_value = "INSERT INTO oc_product_option_value (product_option_id,product_id,option_id,option_value_id,quantity,subtract,price) VALUES ('$latest_prod_option','$checked_product_id','1','$option_value_id','0','0','0') ";
$conn->query($sql_insertion_option_value);
}
}
$sql_deletion_product_to_category = "DELETE FROM oc_product_to_category WHERE product_id='{$checked_product_id}'";
$conn->query($sql_deletion_product_to_category);
/*working in table oc_product_to_category */
$sql_insertion_product_to_category = "INSERT INTO oc_product_to_category (category_id,product_id) VALUES ('{$product_cat_id}','{$checked_product_id}') ";
$conn->query($sql_insertion_product_to_category);
$updated_count++;
}
/********************************************END OF UPDATE*************************************/
else
{
$sql = "INSERT INTO oc_product (model,sku,weight,is_new,price,country,package_size,product_size,volume,package,status,youtube,manufacturer_id,tax_class_id,quantity,stock_status_id)
VALUES ('{$product_barcode}','{$product_code}','{$weight}','{$new_product}','{$price}','{$product_country_name}','{$package_dimensions}','{$product_dimensions}','{$volume}','{$package_num}','{$product_toweb}','{$youtube}',{$brand_id},'9','10000','6')";
if ($conn->query($sql) === true)
{
echo "New record in oc.product created successfully";
}
else
{
echo "Error: " . $sql . "
" . $conn->error;
}
/*******get the generated product id******/
$sql_product_id_create = "SELECT * FROM oc_product WHERE model=$product_barcode ";
if ($result = $conn->query($sql_product_id_create))
{
while ($row = $result->fetch_assoc())
{
$data[] = $row;
}
/*this is the product id for the current product*/
$latest_product_id = $data[0][product_id];
echo $latest_product_id;
echo "
";
/*working in table oc_product_description greek */
$sql_insertion_product_description_gr = "UPDATE oc_product_description SET language_id='3',name='{$product_name}',description='{$product_description}',content='{$contents}',meta_title='{$product_name}',meta_description='{$product_description}' WHERE product_id='{$latest_product_id}'";
$conn->query($sql_insertion_product_description_gr);
if ($conn->query($sql_insertion_product_description_gr) === true)
{
echo "CREATE:New record in oc_product_description UPDATED successfully";
}
else
{
echo "Error oc_product_description: " . $sql . "
" . $conn->error;
}
/*working in table oc_product_description english */
$product_name_eng = str_replace("'", "", $product_name_eng);
$product_description_eng = str_replace("'", "", $product_description_eng);
$sql_insertion_product_description_en = "UPDATE oc_product_description SET language_id='3',name='{$product_name_eng}',description='{$product_description_eng}',content='{$contents_eng}',meta_title='{$product_name_eng}',meta_description='{$product_description_eng}' WHERE product_id='{$latest_product_id}'";
$conn->query($sql_insertion_product_description_en);
if ($conn->query($sql_insertion_product_description_en) === true)
{
echo "CREATE:New record in oc_product_description ENG UPDATED successfully";
}
else
{
echo "Error oc_product_description ENG: " . $sql . "
" . $conn->error;
}
}
/*check if product id exists in table oc_product_to_download */
$sql_oc_product_to_download = "SELECT * FROM oc_product_to_download WHERE product_id=$latest_product_id";
if ($result = $conn->query($sql_oc_product_to_download))
{
while ($row_oc_product_to_download = $result->fetch_assoc())
{
$data_oc_product_to_download[] = $row_oc_product_to_download;
}
$product_to_down = $data_oc_product_to_download[0][product_id];
if ($product_to_down)
{
$sql_delete_product_to_download = "DELETE FROM oc_product_to_download WHERE product_id = {$latest_product_id} ";
$sql_insertion_product_to_download = "INSERT INTO oc_product_to_download (product_id,download_id) VALUES ('{$latest_product_id}','{$odigies}') ";
$conn->query($sql_delete_product_to_download);
$conn->query($sql_insertion_product_to_download);
}
else if ($odigies)
{
$sql_insertion_product_to_download = "INSERT INTO oc_product_to_download (product_id,download_id) VALUES ('{$latest_product_id}','{$odigies}') ";
/*this fires the addition*/
$conn->query($sql_insertion_product_to_download);
}
}
/*working in table oc_product_to_store */
$sql_insertion_product_to_store = "INSERT INTO oc_product_to_store (store_id,product_id) VALUES ('0','{$latest_product_id}') ";
$conn->query($sql_insertion_product_to_store);
/*working in table oc_product_description greek */
$sql_insertion_product_description_gr = "INSERT INTO oc_product_description (product_id,language_id,name,description,content,meta_title,meta_description) VALUES ('$latest_product_id','3','{$product_name}','{$product_description}','{$contents}','{$product_name}','{$product_description}') ";
$conn->query($sql_insertion_product_description_gr);
/*working in table oc_product_description english */
$product_name_eng = str_replace("'", "", $product_name_eng);
$product_description_eng = str_replace("'", "", $product_description_eng);
$sql_insertion_product_description_en = "INSERT INTO oc_product_description (product_id,language_id,name,description,content,meta_title,meta_description) VALUES ('$latest_product_id','1','{$product_name_eng}','{$product_description_eng}','{$contents_eng}','{$product_name_eng}','{$product_description_eng}') ";
$conn->query($sql_insertion_product_description_en);
/*product attributes set up*/
/*gender check the product attr 1 is gender greek is lang id 3 eng is lang id 1 */
/*no worries you can sideload two values*/
if ($sex)
{
if ($sex == "1" || $sex == "3")
{
$gen = 'Αγόρι';
$sql_insertion_gender_attibute_boy = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','1','3','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_boy);
$gen = 'Boy';
$sql_insertion_gender_attibute_en_boy = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','1','1','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_en_boy);
}
if ($sex == 2 || $sex == 3)
{
$gen = 'Κορίτσι';
$sql_insertion_gender_attibute_girl = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','1','3','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_girl);
$gen = 'Girl';
$sql_insertion_gender_attibute_girl_en = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','1','1','{$gen}') ";
$conn->query($sql_insertion_gender_attibute_girl_en);
}
/*iroas add*/
if ($heroe)
{
$sql_insertion_iroes = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','2','3','{$heroe}') ";
$conn->query($sql_insertion_iroes);
$sql_insertion_attibute_en_iroes = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','2','1','{$heroe}') ";
$conn->query($sql_insertion_attibute_en_iroes);
}
if ($puzzle_pieces && $puzzle_pieces != "1")
{
$sql_insertion_puzzle_pieces = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','3','3','{$puzzle_pieces}') ";
$conn->query($sql_insertion_puzzle_pieces);
$sql_insertion_attibute_puzzle_pieces = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','3','1','{$puzzle_pieces}') ";
$conn->query($sql_insertion_attibute_puzzle_pieces);
}
/*add puzzle pieces attribute the attr id is 3*/
if ($puzzle_pieces && $puzzle_pieces != "0")
{
$sql_insertion_puzzle_pieces = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','3','3','{$puzzle_pieces}') ";
$conn->query($sql_insertion_puzzle_pieces);
$sql_insertion_attibute_en_puzzle_pieces = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','3','1','{$puzzle_pieces}') ";
$conn->query($sql_insertion_attibute_en_puzzle_pieces);
}
/*add num players attribute the attr id is 4*/
if ($num_players && $num_players != "0")
{
$sql_insertion_num_players = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','4','3','{$num_players}') ";
$conn->query($sql_insertion_num_players);
$sql_insertion_attibute_en_num_players = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','4','1','{$num_players}') ";
$conn->query($sql_insertion_attibute_en_num_players);
}
/*add num players attribute the attr id is 5*/
if ($gameduration && $gameduration != "0")
{
$sql_insertion_gameduration = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','5','3','{$gameduration}') ";
$conn->query($sql_insertion_gameduration);
$sql_insertion_attibute_en_gameduration = "INSERT INTO oc_product_attribute (product_id,attribute_id,language_id,text) VALUES ('{$latest_product_id}','5','1','{$gameduration}') ";
$conn->query($sql_insertion_attibute_en_gameduration);
}
}
/*manipulation of age*/
$fix_age_range = str_replace("+", "", $age_desciption);
if ($fix_age_range == '1,5' || $fix_age_range == '1.5' || $fix_age_range == '1 1/2')
{
$fix_age_range = 1.5;
}
elseif ($fix_age_range == '2,5' || $fix_age_range == '2.5' || $fix_age_range == '2 1/2')
{
$fix_age_range = 2.5;
}
elseif ($fix_age_range == '' || $fix_age_range == ' ')
{
$fix_age_range = 99;
}
/*gets the product options from the DB*/
$sql_product_option = "SELECT option_value_id,name FROM oc_option_value_description
WHERE name LIKE '%years%'
ORDER BY option_value_id";
if ($result_2 = $conn->query($sql_product_option))
{
while ($row_2 = $result_2->fetch_assoc())
{
$data_2[] = $row_2;
echo "";
/* print_r($data_2);*/
echo "
";
}
/*looping from the DB results and matching*/
foreach ($data_2 as $age_rec)
{
$age_name = explode(' ', $age_rec['name']);
$age_name = explode('-', $age_name[0]);
if ($age_name[0] >= $fix_age_range)
$option_value_ids[] = $age_rec['option_value_id'];
}
}
/*@$option_value_ids is the found ids */
print_r($option_value_ids);
if ($fix_age_range == "99")
{
$fix_age_range = "99";
}
else
{
$fix_age_range = $fix_age_range . "-99";
}
$sql_insertion_attr_age = "INSERT INTO oc_product_option (product_id,option_id,value,required) VALUES ('{$latest_product_id}','1','{$fix_age_range}','1') ";
$conn->query($sql_insertion_attr_age);
$sql_product_option_get_id = "SELECT product_option_id FROM oc_product_option
WHERE product_id = '{$latest_product_id}'";
if ($result_3 = $conn->query($sql_product_option_get_id))
{
while ($row_3 = $result_3->fetch_assoc())
{
$data_3[] = $row_3;
/* echo "";
print_r($data_3);
echo "
";*/
}
}
$latest_prod_option = $data_3[0][product_option_id];
/*echo "test".$fix_age_range;*/
$created_count++;
foreach ($option_value_ids as $option_value_id)
{
/* print_r ($option_value_id);*/
$sql_insertion_option_value = "INSERT INTO oc_product_option_value (product_option_id,product_id,option_id,option_value_id,quantity,subtract,price) VALUES ('$latest_prod_option','$latest_product_id','1','$option_value_id','0','0','0') ";
$conn->query($sql_insertion_option_value);
/*working in table oc_product_to_category */
$sql_insertion_product_to_category = "INSERT INTO oc_product_to_category (category_id,product_id) VALUES ('{$product_cat_id}','{$latest_product_id}') ";
$conn->query($sql_insertion_product_to_category);
}
/*get the categories from erp*/
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'johnhellas.oncloud.gr/S1Services/JS/VitaNET_sync.endpoints/Categories',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"clientID": "' . $arr_resp['clientID'] . '" ,
"appID": "10001"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
) ,
));
$response = curl_exec($curl);
curl_close($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$body_cats = substr($response, $header_size);
$body_cats = iconv('Windows-1253', "UTF-8//TRANSLIT//IGNORE", $body_cats);
echo "";
print_r($body_cats);
echo "
";
/*the product cat id is like the softone id until now*/
}
$product_count++;
$latest_product_id = "";
unset($data_finder);
unset($data);
unset($data_2);
unset($data_3);
unset($option_value_ids);
unset($age_name);
}
/*working in table oc_product_to_category */
$sql_insertion_log = "INSERT INTO erp_log (erp_created,erp_updated) VALUES ('{$created_count}','{$updated_count}') ";
$conn->query($sql_insertion_log);
Bug Fixing on WordPress plugin
Is about the bug about the ivory search extension written in functions.php of the child theme fixing Greek Accents bug.
the code part
/*adds a meta in each product to help the search*/
add_action('save_post_product', 'mp_sync_on_product_save', 10, 3);
function mp_sync_on_product_save( $post_id, $post, $update ) {
$product = wc_get_product( $post_id );
$thetitle=$product->get_title();
$unwanted = array('ά'=>'α', 'έ'=>'ε', 'ή'=>'η', 'ί'=>'ι', 'ό'=>'ο', 'ύ'=>'υ', 'ώ'=>'ω');
$fixed_title_for_search= strtr( $thetitle, $unwanted);
$meta_key = '_searchcactus';
$meta_value = $fixed_title_for_search;
$unique = true;
add_post_meta( $post_id, $meta_key, $meta_value, $unique );
}