No products in the cart.
Select a snippet to display
"1",
"vendor_id" => "1", // Set vendor ID
"source_country" => "8", // Source country ID
"source_city" => "1", // Source city ID
"destination_country" => "3", // Destination country ID
"destination_city" => "1", // Destination city ID
"insurance" => 1, // Assume insurance is 1
"pieces" => 1, // Number of pieces
"length" => 22, // Box length in cm
"width" => 22, // Box width in cm
"height" => 10, // Box height in cm
"gross_weight" => 2, // Total weight
"declared_value" => 100 // Example declared value
]
];
// Build the request parameters
$params = [
'email' => $email,
'private_key' => $private_key,
'domestic_service' => $domestic_service,
'international_service' => $international_service,
'shipment' => json_encode($shipment)
];
// Make the API request
$response = wp_remote_get(add_query_arg($params, $api_url));
// Check if the request was successful
if (is_wp_error($response)) {
return false;
}
// Get the response body and decode it
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
// Check if the API returned a success response
if (isset($data['error_code']) && $data['error_code'] == 508) {
return $data['rate']; // Return the freight charge
}
// Return false if the API response was not successful
return false;
}
// Example package data for testing
$package = [
'destination' => [
'country' => '3',
'city' => '1'
]
];
// Call the function and display the result
$rate = get_dynamic_freight_charge($package);
echo 'Shipping Rate: ' . ($rate !== false ? $rate : 'Error fetching rate');
?>
<?php
