HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/test01.amberconcept/ajax_data.php
<?php
//include wp libriries and set timezone to HK
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('wp-load.php'); 
date_default_timezone_set("Asia/Hong_Kong"); 
global $wpdb;

if($_POST['section']=="get_custom_currency_list"){
	$arr_result = array();
	$arr_result['data'] = array();

	$currency_list = $wpdb->get_results("SELECT * FROM `custom_currency` WHERE status = 1 ORDER BY id");

	$i = 1;
	foreach($currency_list as $currencyObj){		
		array_push($arr_result['data'], 
			array(
				$i,
				"<span style='display:none'>".$currencyObj->currency_code."</span><input type='text' id='".$currencyObj->id."-currency_code' name='".$currencyObj->id."-currency_code' value='".$currencyObj->currency_code."' callout='".$currencyObj->currency_code."' class='text currency_edit'>",
				"<span style='display:none'>".$currencyObj->currency_rate."</span><input type='number' id='".$currencyObj->id."-currency_rate' name='".$currencyObj->id."-currency_rate' value='".$currencyObj->currency_rate."' callout='".$currencyObj->currency_rate."' class='text currency_edit' min='0'>",
				"<i class='text-danger fas fa-3x fa-minus-square remove_currency_btn' style='cursor:pointer' id='remove_currency-".$currencyObj->id."'></i>")
		);
		$i++;
	}
	
	$arr_result['recordsTotal'] = count($arr_result['data']);
	$arr_result['recordsFiltered'] = count($arr_result['data']);
	
	echo json_encode($arr_result);
}

if($_POST['section']=="get_custom_supplier_list"){
	$arr_result = array();
	$arr_result['data'] = array();

	$supplier_list = $wpdb->get_results("SELECT * FROM `custom_supplier` WHERE status = 1 ORDER BY id");
	$i = 1;
	foreach($supplier_list as $supplierObj){		
		array_push($arr_result['data'], 
			array(
				$i,
				"<span style='display:none'>".$supplierObj->supplier_name."</span><input type='text' id='".$supplierObj->id."-supplier_name' name='".$supplierObj->id."-supplier_name' value='".$supplierObj->supplier_name."' callout='".$supplierObj->supplier_name."' class='text supplier_edit'>",				
				"<i class='text-danger fas fa-3x fa-minus-square remove_supplier_btn' style='cursor:pointer' id='remove_supplier-".$supplierObj->id."'></i>")
		);
		$i++;
	}
	
	$arr_result['recordsTotal'] = count($arr_result['data']);
	$arr_result['recordsFiltered'] = count($arr_result['data']);
	
	echo json_encode($arr_result);
}


/*
By Samiel on 2021-08-02
function to adjust order meta (poduct cost/shipping cost/awb no)
*/
if($_POST['section']=="edit_postmeta"){
	$arr_result = array();
	
	if($_POST['post_id']!="" && $_POST['field_name']!="") {
	 	update_post_meta( $_POST['post_id'], $_POST['field_name'], sanitize_text_field( $_POST['nvalue']) );
		$arr_result['id'] = $_POST['post_id']."-".$_POST['field_name'];
		$arr_result['callout'] = $_POST['nvalue'];	
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}


/*
By Samiel on 2021-08-23
function to update shipping cost 
*/

if($_POST['section']=="edit_shippingcost"){
	$arr_result = array();
	$arr_result['id'] = $_POST['key'];
	if(isset($_POST['key']) && $_POST['key']!=""){
		
		$key = explode("-",$_POST['key']);
		$field = $key[0];
		$order_id = $key[1];
		$track_no = $key[2];
		
		$hash = get_post_meta($order_id, '_wc_shipment_tracking_items');
		
		$ship_data = $hash[0];
						
		$updated_ship_data = array();
		foreach ($ship_data as $line){
			
			if($line['tracking_number']==$track_no){			
				$line['shipping_cost'] = $_POST['nvalue'];				
			}
			
			array_push($updated_ship_data, $line);
			
		}
		
		update_post_meta($order_id, '_wc_shipment_tracking_items', $updated_ship_data);
		$arr_result['callout'] = $_POST['nvalue'];	
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	
	
	echo json_encode($arr_result);
}

if($_POST['section']=="edit_supplier"){
	$arr_result = array();	
	
	if($_POST['supplier_id']!="" && $_POST['field_name']!="") {
		$wpdb->update('custom_supplier', array( $_POST['field_name'] => $_POST['nvalue']),array('id'=>$_POST['supplier_id']));
		$arr_result['id'] = $_POST['supplier_id']."-".$_POST['field_name'];
		$arr_result['callout'] = $_POST['nvalue'];	
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="edit_currency"){
	$arr_result = array();
	
	if($_POST['currency_id']!="" && $_POST['field_name']!="") {
		$wpdb->update('custom_currency', array( $_POST['field_name'] => $_POST['nvalue']),array('id'=>$_POST['currency_id']));
		$arr_result['id'] = $_POST['currency_id']."-".$_POST['field_name'];
		$arr_result['callout'] = $_POST['nvalue'];	
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="add_supplier"){
	$arr_result = array();
	
	if($_POST['value']!="") {
		$wpdb->insert('custom_supplier', array(
			'supplier_name' => $_POST['value']
		));
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="add_currency"){
	$arr_result = array();
	
	if($_POST['code']!="" && $_POST['value']!="") {
		$wpdb->insert('custom_currency', array(
			'currency_code' => $_POST['code'],
			'currency_rate' => $_POST['value']
		));
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="remove_supplier"){
	$arr_result = array();
	
	if($_POST['supplier_id']!="") {
		$wpdb->delete("custom_supplier", array( 'id' => $_POST['supplier_id'] ) );
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}

if($_POST['section']=="remove_currency"){
	$arr_result = array();
	
	if($_POST['currency_id']!="") {
		$wpdb->delete("custom_currency", array( 'id' => $_POST['currency_id'] ) );
		$arr_result['condition']='success';
	} else {
		$arr_result['condition']='fail';
	}	
	echo json_encode($arr_result);
}
?>