get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status IN ('{$post_status}') AND post_date LIKE '".$today."%'");
//loop all found orders
foreach ($result as $order_obj ){
$order = wc_get_order($order_obj->ID);
//get order delivery date from order meta table
$delivery_date = $order->get_meta('delivery_date', true);
// if delivery date is today
if($delivery_date == $today ){
//check if the order is already notified
$master_read_record = $wpdb->get_results("SELECT * FROM `notification_master_read` WHERE order_id = '".$order_obj->ID."' LIMIT 1");
if(count($master_read_record)==0){
//check if the order is notified by specific user
$user_read_record = $wpdb->get_results("SELECT * FROM `notification_user_read` WHERE order_id = '".$order_obj->ID."' AND user_id = '".$_POST['user_id']."' LIMIT 1");
if(count($user_read_record)==0){
$order = wc_get_order( $order_obj->ID );
$arr_ord = array();
$arr_ord['order'] = $order_obj->ID;
$arr_ord['product'] = array();
//loop all line item in order
foreach ( $order->get_items() as $item ) {
//get line item product info.
$prod = $item->get_product();
$sku = $prod->get_sku();
// get variation name
$variation_id = $item->get_variation_id();
$variation = new WC_Product_Variation($variation_id);
$variationName = implode(" / ", $variation->get_variation_attributes());
$product = wc_get_product(wc_get_product_id_by_sku($sku));
if($variation_id!=0){
$prod_name = $product->get_name()." - ".$variationName;
}else{
$prod_name = $product->get_name();
}
$arr_item = array();
$arr_item['name'] = $prod_name;
$arr_item['qty'] = $item->get_quantity();
$arr_ord['product'][]= $arr_item;
}
array_push($arr_result,$arr_ord);
}
}
}
}
//return result to ajax call in json format
echo json_encode($arr_result);
}
/*
By Samiel on 2021-03-03 // last edit : 2021-05-11
function to let system know an order notification is already read and do not display again
*/
if($_POST['section']=="remove_notification"){
$json = str_replace("\\","",$_POST['order_id']);
foreach(json_decode($json) as $ord_id){
$wpdb->replace('notification_master_read', array(
'order_id' => $ord_id,
'status' => 1
)
);
}
}
/*
By Samiel on 2021-03-03 // last edit : 2021-05-11
function to let system know an order notification is already read by specific user
*/
if($_POST['section']=="read_notification"){
$json = str_replace("\\","",$_POST['order_id']);
foreach(json_decode($json) as $ord_id){
$wpdb->replace('notification_user_read', array(
'order_id' => $ord_id,
'user_id' => $_POST['user_id'],
'status' => 1
)
);
}
}
/*
By Samiel on 2021-03-11
function to adjust sku stock in sku stock page
*/
if($_POST['section']=="adjust_sku_stock"){
$arr_result = array();
if($wpdb->update('sku_stock', array( $_POST['sku'] => $_POST['nqty']),array('stock_date'=>$_POST['stock_date']))){
$arr_result['condition']='success';
$arr_result['callout'] = $_POST['nqty'];
$arr_result['id'] = $_POST['sku']."__".$_POST['stock_date'];
$product = wc_get_product(wc_get_product_id_by_sku($_POST['sku']));
$sql_statement = "UPDATE sku_stock SET `".$_POST['sku']."` = '".$_POST['nqty']."' WHERE stock_date = '".$_POST['stock_date']."' LIMIT 1";
write_log($_POST['stock_date'], "Adjust Stock", "Back End", "0", $_POST['sku'], esc_sql($product->get_name()), $_POST['oqty'], $_POST['nqty'], $_POST['user_id'], get_client_ip(), $sql_statement);
}else{
$arr_result['condition']='fail';
}
echo json_encode($arr_result);
}
/*
By Samiel on 2021-04-07
function to mark an order is completed in order summary page
*/
if($_POST['section']=="mark_completed"){
$arr_result = array();
$arr_result['id'] = $_POST['ord_id'];
if($_POST['ord_id']!=""){
$orderDetail = new WC_Order( $_POST['ord_id'] );
$orderDetail->update_status("wc-completed", 'Completed', TRUE);
if($_POST['payment']!=""){
if(metadata_exists('post', $_POST['ord_id'], 'Payment')) {
update_post_meta($_POST['ord_id'], 'Payment', $_POST['payment']);
wc_create_order_note($_POST['ord_id'], "Payment: ".sanitize_text_field( $_POST['payment'] ), false, true);
} else {
add_post_meta($_POST['ord_id'], 'Payment', $_POST['payment'], TRUE);
wc_create_order_note($_POST['ord_id'], "Payment: ".sanitize_text_field( $_POST['payment'] ), false, true);
}
}
$arr_result['condition'] = "success";
}else{
$arr_result['condition'] = "fail";
}
echo json_encode($arr_result);
}
/*
By Samiel on 2021-05-07
function to check payment and load a customer payment method list
*/
if($_POST['section']=="check_payment"){
$arr_result = array();
$arr_result['id'] = $_POST['ord_id'];
if($_POST['ord_id']!=""){
$order = new WC_Order( $_POST['ord_id'] );
$arr_result['payment'] = $order->get_payment_method();
$button = "
"; $button .= "
"; $content = "是日店休
"; }else{ $arr_result['notice'] = "It's holiday today
"; } }else{ $arr_key = explode($_POST['id']); $stock_sku = get_master_sku($sku); $unit_qty = get_master_sku_unit_qty($sku); $result = $wpdb->get_results("SELECT `".$stock_sku."` FROM sku_stock WHERE stock_date = '".$date."' LIMIT 1"); $init_info = get_sku_init_info($stock_sku); $end_date = $init_info->end_date; $array = (array) $result[0]; $limit = floor($array[$stock_sku]/$unit_qty); if(check_stock_init($stock_sku, $date)==1){ // within selling period if(count($result)>0){ if(date("Y-m-d") == $date && date("H")>=16){ $arr_result['cartContent'] = "none"; if ($_POST['lang'] == 'chi') { $arr_result['notice'] = "".($isToday?'今天已':'')."售罄
"; }else{ $arr_result['notice'] = "Sold Out ".($isToday?'Today':'')."
"; } }elseif($limit<=0){ $arr_result['cartContent'] = "none"; if($end_date==$date){ // check if last date of selling if ($_POST['lang'] == 'chi') { $arr_result['notice'] = "".($isToday?'今天已':'')."售罄
"; }else{ $arr_result['notice'] = "Sold Out ".($isToday?'Today':'')."
"; } }else{ if ($_POST['lang'] == 'chi') { $arr_result['notice'] = "".($isToday?'今天已':'')."售罄
"; }else{ $arr_result['notice'] = "Sold Out ".($isToday?'Today':'')."
"; } } }elseif($limit<=3){ $arr_result['cartContent'] = "block"; if ($_POST['lang'] == 'chi') { $arr_result['notice'] = "餘".$limit."份
"; }else{ $arr_result['notice'] = "".$limit." Left
"; } }else{ $arr_result['cartContent'] = "block"; if ($_POST['lang'] == 'chi') { $arr_result['notice'] = "有貨
"; }else{ $arr_result['notice'] = "In Stock
"; } } } } elseif(check_stock_init($stock_sku, $date)==0) { $arr_result['cartContent'] = "none"; if ($_POST['lang'] == 'chi') { $arr_result['notice'] = "限定期間已售完
"; }else{ $arr_result['notice'] = "Sales Ended
"; } } elseif(check_stock_init($stock_sku, $date)==2) { $arr_result['cartContent'] = "none"; if ($_POST['lang'] == 'chi') { $arr_result['notice'] = "即將推出
"; }else{ $arr_result['notice'] = "Coming Soon
"; } } } $arr_result['cartContentID'] = "cart_".$product_id; //$arr_result['date'] = $date; //$arr_result['sku'] = $sku; //$arr_result['msku'] = get_master_sku($sku); echo json_encode($arr_result); } ?>