John Davidson

php - Courier API integration not working on woocommerce orders page

0 comments
Message:


Hi have created a API with the following courier API doc -https://documenter.getpostman.com/view/2869886/T1LLDTRq#0eced6d0-4b19-4ad0-9430-a984ff507539


A button to download a waybill is supposed to appear on the orders page the weird thing is that it was working a few weeks ago not sure why it's not working?


Dawnwing Services
The Dawnwing RESTful JSON API is an API that can be used by external programs to speak to our system. It can, amongst other things, create waybills, Create Parcels, Create tracking And book collections.


Base URL:


UAT https://swatws.dawnwing.co.za/dwwebservices/v2/uat/api/


LIVE https://swatws.dawnwing.co.za/dwwebservices/v2/live/api/


<?php


/***********************************thankyou page generate order url*********************************************/

add_action('woocommerce_order_status_shipped', 'wdm_send_order_to_ext');
function wdm_send_order_to_ext( $order_id ){
// get order object and order details
$order = new WC_Order( $order_id );
$email = $order->billing_email;
$phone = $order->billing_phone;
$shipping_type = (array)$order->get_shipping_method();
$order_number = $order->order_number;
if(!empty( $order->get_items( 'shipping' ))){
$shipping_method_instance_id ='';
foreach( $order->get_items( 'shipping' ) as $item_id => $shipping_item_obj ){
$shipping_method_instance_id .= $shipping_item_obj->get_instance_id();
}
}
$instance_id = $shipping_method_instance_id;
if( $instance_id == 1 )
{
$_shipping_type = "ONX";
}
else{
$_shipping_type = "ECON";
}

$shipping_cost = $order->get_total_shipping();

// set the address fields//
$user_id = $order->user_id;
$address_fields = array('country',
'title',
'first_name',
'last_name',
'company',
'address_1',
'address_2',
'address_3',
'address_4',
'city',
'state',
'postcode');

$address = array();
if(is_array($address_fields)){
foreach($address_fields as $field){
$address['billing_'.$field] = get_user_meta( $user_id, 'billing_'.$field, true );
$address['shipping_'.$field] = get_user_meta( $user_id, 'shipping_'.$field, true );
}
}


// get product details//
$items = $order->get_items();

$item_name = array();
$item_qty = array();
$item_price = array();
$item_sku = array();

foreach( $items as $key => $item){
$item_name[] = $item['name'];
$item_qty[] = $item['qty'];
$item_price[] = $item['line_total'];

$item_id = $item['product_id'];
$product = new WC_Product($item_id);
$item_sku[] = $product->get_sku();
}

$customer_note = $order->get_customer_note();
$transaction_key = get_post_meta( $order_number, '_transaction_id', true );
$transaction_key = empty($transaction_key) ? $_GET['key'] : $transaction_key;

// set the username and password//
$api_username = 'test';
$api_password = 'test';
// to test out the API, set $api_mode as ‘sandbox’//

// production URL example//


// setup the data which has to be sent//

$datawaybill = [
"waybillNo"=> $order_number,
"sendAccNo"=> "CPT3685",
"sendSite"=> "CPT3685ONL",
"sendCompany"=> "LITTLE BRAND BOX",
"sendAdd1"=> "101 Bree Castle House",
"sendAdd2"=> "68 Bree Street",
"sendAdd3"=> "",
"sendAdd4"=> "Cape Town",
"sendAdd5"=> "8000",
"sendContactPerson"=> "Zak",
"sendHomeTel"=> null,
"sendWorkTel"=> "0214236868",
"sendCell"=> null,
"recCompany"=> "",
"recAdd1"=> $order->get_shipping_address_1(),
"recAdd2"=> $order->get_shipping_address_2(),
"recAdd3"=> $order->get_shipping_city(),
"recAdd4"=> $order->get_shipping_state(),
"recAdd5"=> $order->get_shipping_postcode(),
"recAdd7"=> $company,
"recContactPerson"=> $order->get_shipping_first_name().' '.$order->get_shipping_last_name(),
"recHomeTel"=> "",
"recWorkTel"=> $phone,
"recCell"=> $phone,
"specialInstructions"=> $order->get_customer_note(),
"serviceType"=> $_shipping_type,
"totQTY"=> 1,
"totMass"=> 1,
"insurance"=> false,
"insuranceValue"=> 0,
"customerRef"=> $order_number,
"storeCode"=> "LBB",
"securityStamp"=> null,
"requiredDocs"=> [],
"waybillInstructions"=> [],
"instructionCode"=> "",
"isSecureDelivery"=> false,
"verificationNumbers"=> null,
"generateSecurePin"=> false,
"collectionNo"=> null,
"invoiceRef"=> null,
"parcels"=> [
[
"waybillNo"=> $order_number,
"length"=> 26,
"height"=> 5,
"width"=> 23,
"mass"=> 1,
"parcelDescription"=> "HAIR HEALTH PRODUCTS",
"parcelNo"=> $order_number . "_1",
"parcelCount"=> 1
]
],
"completeWaybillAfterSave"=> true
];

// send API request via cURL
$ch = curl_init();

// set the complete URL, to process the order on the external system. Let’s consider http ->//example.com/buyitem.php is the URL, which invokes the API //
curl_setopt($ch, CURLOPT_URL, 'http://swatws.dawnwing.co.za/dwwebservices/V2/live/api/waybill');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($datawaybill));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Type: application/json'));
$response = curl_exec ($ch);

curl_close ($ch);

// the handle response
if (strpos($response,'ERROR') !== false) {
print_r('eror');
} else {
$datacompletewaybill = array(
'waybillNo' => $order_number,
'storeCode' => "LBB",
'securityStamp' => '',
'generateLabel' => true,
);

// send API request via cURL
$ch = curl_init();

// set the complete URL, to process the order on the external system. Let’s consider http ->//example.com/buyitem.php is the URL, which invokes the API //
curl_setopt($ch, CURLOPT_URL, 'http://swatws.dawnwing.co.za/dwwebservices/V2/live/api/waybill/completewaybill');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($datacompletewaybill));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Type: application/json') );
$response = curl_exec ($ch);

curl_close ($ch);
$response_array = json_decode($response);
if(!empty($response_array)):
update_post_meta($order_id, 'my_field_order_url', $response_array->data[0] );

endif;
}

}


/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ){
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
if($order->get_status() == 'shipped' || $order->get_status() == 'completed' ):
echo '<p class="my_field_order_url" style="display:none;">' . get_post_meta( $order_id, 'my_field_order_url', true ) . '</p>';
endif; ?>
<script>
jQuery(document).ready(function() {
var htmls = jQuery('p.my_field_order_url').html();
var lengths = jQuery('p.my_field_order_url').length;
if (htmls !== '' && lengths > 0) {
jQuery('<div id="yith-order-traking-url" class="postbox"><div class="inside"><a class="wayb" href="' +
htmls + '" target="_blank">Download Waybill</a></div></div>').insertAfter(
'#yith-order-tracking-information');
}
jQuery('div#postcustomstuff table tbody tr td').each(function() {
if (jQuery(this).find('input').val() === 'my_field_order_url') {
jQuery(this).remove();
}
if (jQuery(this).find('textarea').val() ===
'<?=get_post_meta( $order_id, 'my_field_order_url', true )?>') {
jQuery(this).remove();
}
});
});
</script>
<style>
a.wayb {
background: orange;
float: left;
margin: 0px !important;
padding: 10px;
width: 100%;
text-align: center;
color: white;
text-decoration: none;
font-size: 16px;
}


div#yith-order-traking-url .inside {

height: 42px;
margin: 0px;
padding: 0px !important;
border: 9ox !important;
width: 100% !important;
}

</style>


My Answer:
To integrate this courier API with your WooCommerce orders page, you will need to follow these steps:

1. Obtain API credentials: Make sure you have obtained the necessary API credentials (such as API key, username, password, etc.) from the courier service provider.

2. Install a plugin: You can use a plugin like "WooCommerce Shipment Tracking" or "WooCommerce Shipping API" to integrate the courier API with your WooCommerce orders page.

3. Configure the plugin: Once you have installed the plugin, go to the settings page and enter the API credentials provided by the courier service provider.

4. Test the integration: Create a test order on your WooCommerce store and check if the courier API is able to fetch the shipping details and update the order status accordingly.

5. Troubleshooting: If you encounter any issues during the integration process, refer to the API documentation provided by the courier service provider or reach out to their support team for assistance.

By following these steps, you should be able to successfully integrate the courier API with your WooCommerce orders page and streamline the shipping process for your customers.

Rate this post

5 of 5 based on 2291 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us