add_action('woocommerce_thankyou', 'send_order_to_courier_system', 10, 1); function send_order_to_courier_system($order_id) { if (!$order_id) return; // Get order details $order = wc_get_order($order_id); $items = $order->get_items(); $total_weight = 0; // Calculate total weight (optional) foreach ($items as $item) { $product = $item->get_product(); $total_weight += $product->get_weight() * $item->get_quantity(); } // Prepare data for API $shipment_data = [ 'tracking_number' => $order->get_order_number(), 'sender_name' => get_bloginfo('name'), 'sender_address' => get_option('woocommerce_store_address') . ' ' . get_option('woocommerce_store_city'), 'sender_phone' => get_option('woocommerce_store_phone'), 'consignee_name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(), 'consignee_city' => $order->get_billing_city(), 'consignee_phone' => $order->get_billing_phone(), 'package_weight' => $total_weight, 'package_description' => 'WooCommerce Order #' . $order_id, 'cod_amount' => $order->get_total(), 'quantity' => $order->get_item_count(), 'reference_no' => $order->get_id(), 'service_type' => 'Standard', 'parcel_type' => 'Box', ]; // Courier System API Endpoint $courier_api_url = 'https://your-courier-system.com/api/create-shipment'; // Send data via cURL $response = wp_remote_post($courier_api_url, [ 'method' => 'POST', 'body' => json_encode($shipment_data), 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer YOUR_API_KEY', // Optional if using auth ], ]); // Check response (optional) if (is_wp_error($response)) { error_log('Courier System Error: ' . $response->get_error_message()); } else { error_log('Order #' . $order_id . ' sent to Courier System.'); } }
New WordPress website is being built and will be published soon