6

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.

Tom Hanson
  • 873
  • 10
  • 39

1 Answers1

1

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.