function wc_place_orders($request) {
global $woocommerce, $wpdb, $post;
$Userid = $request['user_id'];
$PayMethod = $request['payment'];
$Fname = $request['first_name'];
$Lname = $request['last_name'];
$Email = $request['email'];
$Phone = $request['phone'];
$Add1 = $request['address_1'];
$Add2 = $request['address_2'];
$City = htmlspecialchars($request['city'], ENT_QUOTES);
$State = $request['state'];
$PostCode = $request['postcode'];
$Country = $request['country'];
$Coupon = $request['coupon_code'];
//Get cart items
$i = 0;
$saved_cart_meta = get_user_meta($request['user_id'], '_woocommerce_persistent_cart_' . get_current_blog_id(), true);
foreach ($saved_cart_meta['cart'] as $key => $value) {
$line_total[] = $value['line_subtotal'] * $value['quantity'];
$line_tax[] = $value['line_subtotal_tax'] * $value['quantity'];
$pid = $value['product_id'];
$qty = $value['quantity'];
$test6[] = "line_items[$i][product_id]=$pid&line_items[$i][quantity]=$qty";
$i++;
}
$lines = implode( '&',$test6 );
$line_total1 = array_sum($line_total);
$line_tax1 = array_sum($line_tax);
$totalsz = $line_total1 + $line_tax1;
//get coupon code
$coupon = new WC_Coupon($request['coupon_code']);
$coupon_post = get_post($coupon->id);
//get shipping zone
$delivery_zones = WC_Shipping_Zones::get_zones();
foreach ((array) $delivery_zones as $key => $the_zone) {
foreach ($the_zone['shipping_methods'] as $value) {
if (!$value->cost == '') {
$shipping_rate = $value->cost;
}
}
}
//get tax from zone id
$ShipTax = WC_Tax::_get_tax_rate(4);
$Tx = $ShipTax['tax_rate'];
$shippingIncTx = $shipping_rate * $Tx / 100;
//cases for add shipping rates
if($totalsz < 25 && $coupon->enable_free_shipping() == false){
$ShipppingCharge = $shipping_rate;
$TotalShipping = $shipping_rate + round($shippingIncTx, 2);
$case = 1;
} elseif($totalsz < 25 && $coupon->enable_free_shipping() == true) {
$ShipppingCharge = 0;
$TotalShipping = 0;
$case = 2;
} elseif($totalsz < 25){
$ShipppingCharge = $shipping_rate;
$TotalShipping = $shipping_rate + round($shippingIncTx, 2);
$case = 3;
} else {
$ShipppingCharge = 0;
$TotalShipping = 0;
$case = 4;
}
//check for coupons
$is_applied = false;
if($Coupon == ''){
$IsApply = '';
} else {
$IsApply = "&coupon_lines[0][code]=$Coupon";
$is_applied = true;
}
//post order data using curl
$ch = curl_init();
$url = "https://stomerijcollectief.nl/wp-json/wc/v3/orders/?consumer_key=ck_61589ef3bf68cb47f52a8e74d37717e7b76c1000&consumer_secret=cs_b6699d670e27e2cf7e0bb3044ca1e6c10f0fe574"; // Where you want to post data
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "status=processing&customer_id=$Userid&payment_method=$PayMethod&payment_method_title=Cash on Delivery&billing[first_name]=$Fname&billing[last_name]=$Lname&billing[address_1]=$Add1 Market&billing[address_2]=$Add2&billing[city]=$City&billing[state]=$State&billing[postcode]=$PostCode&billing[country]=$Country&billing[email]=$Email&billing[phone]=$Phone&$lines&shipping_lines[0][method_id]=flat_rate&shipping_lines[0][method_title]=gratis bezorging&shipping_lines[0][total]=$ShipppingCharge" . $IsApply);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);
$ts = json_decode($output);
$order_id = $ts->id;
$discount = $ts->discount_total + $ts->discount_tax;
//getting order details by id
$order = wc_get_order($order_id);
$order_data = $order->get_data(); // The Order data
$subtotal = $order->get_subtotal();
$order_data = $order->get_data();
$order_id = $order_data['id'];
$order_name = $order_data['billing']['first_name'] . ' ' . $order_data['billing']['last_name'];
$order_email = $order_data['billing']['email'];
$order_address = $order_data['billing']['address_1'];
$order_city = $order_data['billing']['city'];
$order_postcode = $order_data['billing']['postcode'];
$order_total = $order->get_total();
$order_order_key = $order_data['order_key'];
foreach ($order->get_items() as $item_id => $item_data) {
$product_name .= $item_data['name'] . '- ';
$item_quantity[] = $order->get_item_meta($item_id, '_qty', true);
}
$products_name = rtrim($product_name, " -");
$qty = array_sum($item_quantity);
//Getting pick/drop timings if user go for cart directly
if ($request['pickup_date'] == '' && $request['drop_date'] == '') {
$zipz = substr($order_postcode, 0, 4);
$args = array(
'post_type' => 'product',
's' => $zipz
);
$loop = new WP_Query($args);
if ($loop->have_posts()) :
while ($loop->have_posts()) : $loop->the_post();
$pickup_date = get_field('pickup_date', get_the_ID()) . ' ' . get_post_meta(get_the_ID(), 'time_slot_from', true) . '-' . get_post_meta(get_the_ID(), 'time_slot_to', true);
$drop_date = get_field('dropoff_date', get_the_ID()) . ' ' . get_post_meta(get_the_ID(), 'drop_time_slot_from', true) . '-' . get_post_meta(get_the_ID(), 'drop_time_slot_to', true);
add_post_meta($order_id, 'pickup_date', $pickup_date);
add_post_meta($order_id, 'drop_date', $drop_date);
endwhile;
endif;
} else {
add_post_meta($order_id, "pickup_date", $request['pickup_date']);
add_post_meta($order_id, "drop_date", $request['drop_date']);
$pickup_date = get_post_meta($order_id, 'pickup_date', true);
$drop_date = get_post_meta($order_id, 'drop_date', true);
}
$phone = $order_data['billing']['phone'];
//post data into custom table (optional)
$chk = $wpdb->get_results("SELECT * FROM `order_details` WHERE `order_id` = '$order_id'");
if (empty($chk)) {
if ($order->get_payment_method() == 'cod') {
$add_activity = $wpdb->query("INSERT INTO `order_details`(`order_id`, `order_name`, `order_email`, `order_address`, `order_city`, `order_postcode`, `order_total`,`sub_total`, `order_order_key`, `product_name`, `item_quantity`,`phone`, `pick_time`, `drop_time`) VALUES ('$order_id', '$order_name', '$order_email', '$order_address', '$order_city', '$order_postcode', '$order_total', '$subtotal', '$order_order_key', '$products_name', '$qty','$phone', '$pickup_date', '$drop_date')");
} else {
$add_activity = $wpdb->query("INSERT INTO `order_details`(`order_id`, `order_name`, `order_email`, `order_address`, `order_city`, `order_postcode`, `order_total`,`sub_total`, `order_order_key`, `product_name`, `item_quantity`,`phone`, `pick_time`, `drop_time`, `status`) VALUES ('$order_id', '$order_name', '$order_email', '$order_address', '$order_city', '$order_postcode', '$order_total', '$subtotal', '$order_order_key', '$products_name', '$qty','$phone', '$pickup_date', '$drop_date', '1')");
}
}
//Empty cart
delete_user_meta($request['user_id'], '_woocommerce_persistent_cart_1', '');
delete_user_meta($request['user_id'], '_usertypes', '');
delete_user_meta($request['user_id'], '_booking_timings', '');
$GetData = $wpdb->get_results("SELECT * FROM `order_details` WHERE `order_id` = '$order_id'");
$EditSubtotal = $subtotal * 21/100;
$EditedSubT = $EditSubtotal + $subtotal;
$CouponCheck = ['is_applied' => $is_applied, 'discount_rate' => $discount, 'subtotal' => $EditedSubT, 'shipping' => $TotalShipping, 'case' => $case];
$FinalData = array_merge($GetData, $CouponCheck);
return new WP_REST_Response($FinalData);
}
//register route for order place
add_action('rest_api_init', function () {
register_rest_route('wp/v2', '/place_orders', array(
'methods' => 'POST',
'callback' => 'wc_place_orders', //Function name
));
});