bfpl-custom.php000064400000007633150212660770007527 0ustar00prefix . 'cart'; $output = array(); $username = base64_decode($data['id']); $getCart = $wpdb->get_results("SELECT product_id, quantity FROM `$table_name` where user='".$username."' "); if($getCart){ foreach($getCart as $getCart ){ $output[] = array( 'product_id' => $getCart ->product_id, 'quantity' => $getCart ->quantity, ); } } //return "SELECT product_id, quantity FROM `$table_name` where user='".$data['id']."' "; return json_encode( $output, JSON_NUMERIC_CHECK); //return $posts[0]->post_title.'Hello World'.$data['id']; //return $username; } add_action( 'rest_api_init', function () { register_rest_route( 'bfpl-custom/v1', '/cart/(?P[a-zA-Z0-9-]+)', array( 'methods' => 'GET', 'callback' => 'get_cart_products', ) ); } ); function get_cart_calculation($data){ global $wpdb; $table_name = $wpdb->prefix . 'cart'; $output = array(); $sumTotal=0; $output = array(); $username = base64_decode($data['id']); $getCart = $wpdb->get_results("SELECT product_id, quantity FROM `$table_name` where user='".$username."'"); if($getCart){ foreach($getCart as $getCart ){ $_product = wc_get_product( $getCart ->product_id ); $productPrice = $_product->get_regular_price(); $sumTotal = $sumTotal + ($productPrice * $getCart ->quantity); } $shippingPrice = 20; $tax = ($sumTotal + $shippingPrice) * 5/100; $payPrice = $sumTotal + $tax + $shippingPrice; $output[] = array( 'sumtotal' => $sumTotal, 'shippingprice' => $shippingPrice, 'tax' => $tax, 'payprice' => $payPrice ); } return json_encode( $output, JSON_NUMERIC_CHECK); } add_action( 'rest_api_init', function () { register_rest_route( 'bfpl-custom/v1', '/cartcalculation/(?P[a-zA-Z0-9-]+)', array( 'methods' => 'GET', 'callback' => 'get_cart_calculation', ) ); } ); function handle_cart( $request ) { global $wpdb; $table_name = $wpdb->prefix . 'cart'; $item = $request->get_params(); $getCart = $wpdb->get_results("SELECT `id` as idcount FROM `$table_name` where user='".$item['user']."' and product_id = '".$item['product_id']."'"); if($getCart){ $query = "UPDATE `$table_name` SET quantity = quantity + ".$item['quantity']." where user='".$item['user']."' and product_id = '".$item['product_id']."'"; }else{ $query = "INSERT INTO `$table_name` (`user`, `product_id`, `quantity`) VALUES ('".$item['user']."', ".$item['product_id'].", ".$item['quantity'].")"; } $list = $wpdb->get_results($query); //$fields = array(); //$values = array(); //foreach($item as $key => $val) { // array_push($fields, preg_replace("/[^A-Za-z0-9]/", '', $key)); // array_push($values, $wpdb->prepare('%s', $val)); //} //$fields = implode(", ", $fields); //$values = implode(", ", $values); //$query = "INSERT INTO `$table_name` ($fields) VALUES ($values)"; //$list = $wpdb->get_results($query); //return "Success"; return $query; } add_action( 'rest_api_init', function () { register_rest_route( 'bfpl-custom/v1', '/cart-entry', array( 'methods' => 'POST', 'callback' => 'handle_cart', 'permission_callback' => function () { return current_user_can( 'customer' ); } ) ); } ); ?>