Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Order Details From WooCommerce To External System Over API

I am trying to send woocommerce order to netsuite via an external api I have written. I am nw to woocommerce and do not fully get how to add this functionality.

I have added the following code to the functions.php file in public_html/wp-content/themes/reverie-master/

add_action( 'woocommerce_payment_complete'', '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;

//Create the data object
$orderData = array(
    'customer_email' => $email,
    'customer_phone' => $phone
);

$apiData = array(
    'caller' => 'woocommerce',
    'json' => $orderData,
    'key' => 'MY_SECRET_KEY'
);

$jsonData =json_encode($orderData);

$url = "";
$api_mode = 'sandbox';
if($api_mode == 'sandbox'){
    // sandbox URL example
    $url = "https://forms.netsuite.com/app/site/hosting/scriptlet.nl?script=XXX&deploy=X&compid=XXXXXXX_SB1&h=XXXXXXXXXXXXXXXX"; 
}
else{
    // production URL example
    $url = ""; 
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($jsonData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec ($ch);

curl_close ($ch);

// the handle response    
if (strpos($response,'ERROR') !== false) {
        print_r($response);
} else {
        // success
}
}

I have tested the brunt of this code, just the parts that do not concern woocommerce in a different site and I can see the data showing up in NetSuite. However, when I go through my store and place an order, and take payment, I do not see the data come into NetSuite. Do I have this code in the right location? Is there something I am missing?

Update I installed the plugin Code Snippets and added the code there instead. Set it to Run snippet everywhere. Still no luck.

like image 518
Tom Hanson Avatar asked Mar 22 '19 03:03

Tom Hanson


People also ask

How do I integrate API with WooCommerce?

Step 1: Log in to the backend of your WordPress website. Step 2: Hover over “WooComerce”, select “Settings”, and then “Advanced”. Step 3: Toggle the “Legacy API” tab and activate the “Enable the legacy REST API” button. The WooCommerce API is now enabled.

What can you do with WooCommerce REST API?

WooCommerce (WC) 2.6+ is fully integrated with the WordPress REST API. This allows WC data to be created, read, updated, and deleted using requests in JSON format and using WordPress REST API Authentication methods and standard HTTP verbs which are understood by most HTTP clients.

How do I integrate an external API in WordPress?

Go to the Connect To External API tab in the plugin to connect the External/third-party provider's API endpoints to WordPress. Select the POST method from the Select Method dropdown. In the External API textbox, put the API endpoint that you want to connect it with WordPress.


1 Answers

Looks like you have a double quote on the first link

change

add_action( 'woocommerce_payment_complete'', 'wdm_send_order_to_ext');

to

add_action( 'woocommerce_payment_complete', 'wdm_send_order_to_ext');

Rather than use curl - you can always use the build in WordPress wp_remote_post() function

Also make sure you have WP_DEBUG set to true in wp-config.php while testing.

like image 103
Coder At Heart Avatar answered Jan 04 '23 14:01

Coder At Heart