while i am developing WooCommerce website in WordPress for order tracking i need to integrate with xpressbees, and the orders to be automated in xpressbees and the tracking report to be processed in woocommerce orders. here xpressbees not having woocommerce channel creation i had tried using custom api end point process but it is not working
$order->get_id(),
‘order_amount’ => $order->get_total(),
‘customer_name’ => $order->get_billing_first_name() . ‘ ‘ . $order->get_billing_last_name(),
‘customer_address’ => $order->get_billing_address_1(),
‘customer_phone’ => $order->get_billing_phone(),
‘customer_email’ => $order->get_billing_email(),
‘items’ => array()
);
foreach ($order->get_items() as $item_id => $item) {
$product = $item->get_product();
$order_data[‘items’][] = array(
‘product_id’ => $product->get_id(),
‘product_name’ => $product->get_name(),
‘quantity’ => $item->get_quantity(),
‘price’ => $item->get_total()
);
}
error_log(‘Order Data: ‘ . print_r($order_data, true));
$api_url = ‘https://ship.xpressbees.com/api/users/franchise_user_login’;
$api_key = ‘YOUR_API_KEY’; // Replace with your actual API key
$response = wp_remote_post($api_url, array(
‘body’ => json_encode($order_data),
‘headers’ => array(
‘Authorization’ => ‘Bearer ‘ . $api_key,
‘Content-Type’ => ‘application/json’
)
));
if (is_wp_error($response)) {
error_log(‘Error contacting XpressBees API: ‘ . $response->get_error_message());
return;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
error_log(‘Error parsing XpressBees API response: ‘ . json_last_error_msg());
return;
}
error_log(‘XpressBees API Response: ‘ . print_r($data, true));
if (isset($data[‘tracking_number’])) {
update_post_meta($order_id, ‘_tracking_number’, $data[‘tracking_number’]);
update_post_meta($order_id, ‘_shipping_provider’, ‘XpressBees’);
} else {
error_log(‘Tracking number not found in XpressBees response: ‘ . $body);
}
}
add_action(‘woocommerce_admin_order_data_after_order_details’, ‘display_tracking_information’);
function display_tracking_information($order) {
$tracking_number = get_post_meta($order->get_id(), ‘_tracking_number’, true);
$shipping_provider = get_post_meta($order->get_id(), ‘_shipping_provider’, true);
if ($tracking_number && $shipping_provider) {
echo ‘
‘ . __(‘Tracking Number’) . ‘: ‘ . esc_html($tracking_number) . ‘
‘;
echo ‘
‘ . __(‘Shipping Provider’) . ‘: ‘ . esc_html($shipping_provider) . ‘
‘;
}
}
this is what i had included to automate the orders and also i had replaced the api key but it is not working
Vikas V is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.