Skip to content

Instantly share code, notes, and snippets.

@designloud
Last active February 11, 2020 04:53
Show Gist options
  • Save designloud/b4fc2984650cd82e988231436bac6346 to your computer and use it in GitHub Desktop.
Save designloud/b4fc2984650cd82e988231436bac6346 to your computer and use it in GitHub Desktop.
An example of connecting WooCommerce to the SWELLEnterprise API. This example creates a new client in SWELLEnterprise when a new order is placed in WooCommerce. You can expand on this to also create a note with order details, leads, etc. **Don't forget to change to your SWELL API key**
<?php
//Add to plugin or themes functions.php file
//Swell System add order
add_action( 'woocommerce_order_status_completed', 'wc_send_order_to_swell');
function wc_send_order_to_swell( $order_id ) {
$order = wc_get_order($order_id);
$order_data = $order->get_data();
$apiurl = 'https://app.swellsystem.com/api/clients';
$token = ''; //add token here
// Define an empty array for SWELL data
$data = array();
// Store the data into the array, you have access to the $order object
$data["id"] = $order_data['customer_id'];
$data["first_name"] = $order_data['billing']['first_name'];
$data['last_name'] = $order_data['billing']['last_name'];
$data['address'] = $order_data['billing']['address_1'];
$data['city'] = $order_data['billing']['city'];
$data['zip'] = $order_data['billing']['postcode'];
$data['phone_number'] = $order_data['billing']['phone'];
$data['state'] = $order_data['billing']['state'];
$data['email'] = $order_data['billing']['email'];
// Encode the data to be sent
$json_data = json_encode($data);
// Initiate the cURL API call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
$headers[] = 'Authorization:Bearer ' . $token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment