File: /var/www/dk/wp-content/themes/food/api/cart/r_cart.php
<?php
$user = wp_get_current_user();
$uid = $user->ID;
if(!empty($uid) && $uid > 0) {
}else {
$rc = 1;
return false;
}
$cart_res = $wpdb->get_results($wpdb->prepare("SELECT * from food_cart where `uid` = %d and `status` = 1", $uid));
$res = new stdClass;
$res->cart_list = [];
$res->total = 0;
foreach ($cart_res as $key => $value) {
$args = array( 'include' => array( $value->product_id ) );
$products = wc_get_products( $args );
if(count($products) > 0) {
$product = $products[0];
$imageId = $product->get_image_id();
$image_arr = wp_get_attachment_image_src($imageId,"full");
if (!empty($image_arr) && is_array($image_arr)){
$image = $image_arr[0];
} else {
$image = "";
}
$arr=$product->get_category_ids();
if (count($arr)>=2){
$catelist=[];
foreach ($arr as $a){
$std = new stdClass();
$std=$wpdb->get_var($wpdb->prepare("select name from `wp_terms` where term_id=%d",$a));
array_push($catelist,$std);
}
$cate_single = $catelist[0];
}else{
$cate_single = $wpdb->get_var($wpdb->prepare("select name from `wp_terms` where term_id=%d",$product->get_category_ids()));
}
$regular_price = empty($product->get_regular_price())?'0':$product->get_regular_price();
$res->total = $res->total + ($value->product_num * $regular_price);
$res->cart_list[] = [
'id' => $value->id,
'uid' => $value->uid,
'product_id' => $value->product_id,
'product_num' => intval($value->product_num),
'status' => $value->status,
'product_name' => $product->get_name(),
'img_url' => $image,
'cate_single' => $cate_single,
'regular_price' => $regular_price
];
}
}