I created custom PHP file in WordPress to send order data to an accounting server via its web service.
Now I need to take amount of goods from that accounting server and update the stock of each product.
I do not access that server, and they don't have any document for their web-service.
I tried to find some info about using web service to do this, but the only thing that I got was to send (not receiving) data of a WordPress site via web service.
This is my code that send order details to accounting server:
<?php
require_once('../wp-config.php');
require_once('../wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-order.php');
$today = new DateTime();
$today->setTime( 0, 0, 0 );
$args = array(
'limit' => 9999,
'return' => 'ids',
'date_completed' => $today,
'status' => 'completed'
);
$query = new WC_Order_Query( $args );
$orders = $query->get_orders();
$url = "http://###.###.###.###:8081/Service1.svc/InsertInvoice";
foreach( $orders as $order_id ) {
$order = wc_get_order( $order_id );
$order_date_completed = $order->get_date_completed();
if (false === strtotime($order_date_completed)) {
echo 'Invalid date for order number '.$order_id.'<br>';
}else {
$order_date_completed->setTime( 0, 0, 0 );
$diff = $today->diff( $order_date_completed );
$diffDays = (integer)$diff->format( "%R%a" );
if($diffDays == 0){
$items = $order->get_items();
$InvoiceInfo = array();
foreach( $items as $item ){
$product = $item->get_product();
$item_sku = $product->get_sku();
$saleType = substr($item_sku, 1, 2);
$item_code = substr($item_sku, 3, 8);
$user_id = substr($item_sku, 11, 6);
$data2 = array(
'CustomerCode' => $user_id,
'SaleTypeNumber' => $saleType,
'ItemCode' => $item_code,
'Quantity' => $item->get_quantity(),
'Fee' => $product->get_price(),
'Discount' => 0,
'Addition' => 0,
'Tax' => 0,
'Duty' => 0,
'Rate' => 1,
'StockCode' => 5,
);
array_push($InvoiceInfo,$data2);
}
$postdata = json_encode($InvoiceInfo);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
}
}
}
?>
In order to send data to WooCommerce, you can use WordPress REST API custom endpoint that can receive data from anywhere.
But, as you didn't provide the name of the accounting server or even any information about the accounting app, you have to check if it is possible to send data from your accounting app to a webhook (with a service like Zapier).
Related
I want to redirect my buyers to my second website 'B' to make payment for my product on website 'A' and after successful payment on website 'B', it should automatically update the order status on website 'A' as the order is completed. I am using woo commerce plugin for my websites. Please any plugins, Add-ons, or custom code suggestions. please I need help
Site 'A'
function action_woocommerce_new_order( $order_id ) {
//get all order details by $order_id
//post your data to site B
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://yoursiteB.com/wp-json/api/v2/create_wc_order");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2");
// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
};
add_action( 'woocommerce_payment_complete', 'action_woocommerce_new_order', 10, 1 );
Site 'B'
Class WC_Rest_API {
public function __construct() {
add_action( 'rest_api_init', array( $this, 'init_rest_api') );
}
public function init_rest_api() {
register_rest_route('api/v2', '/create_wc_order', array(
'methods' => 'POST',
'callback' => array($this,'create_wc_order'),
));
}
public function create_wc_order( $data ) {
//$data - all the details needs to be send from site A
global $woocommerce;
$user_id = $data['user_id'];
$user = get_user_by( 'ID', $user_id);
$phonenumer = get_user_meta( $user_id,'user_phoneno', true);
if($user) {
$address = array(
'first_name' => $user->display_name,
'last_name' => $user->display_name,
'email' => $user->user_email,
'phone' => $phonenumer,
);
$order = wc_create_order();
//get product details from rest api site A
$order->add_product();
$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order = wc_get_order( $order->id );
$response['status'] = 'success';
$response['message'] = 'Order created on wordpress site B.';
return new WP_REST_Response($response, 200);
}
}
}
$rest_api = new WC_Rest_API();
this doesn't work as it create product on the second website.
I want to use the second website to make only payment if it can pull user data and order id only
I need to export woocommerce order data using a POST request to a third-party url.I have this code in functions.php
add_action('woocommerce_thankyou', '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->get_billing_email();
$phone = $order->get_billing_phone();
$shipping_type = $order->get_shipping_method();
$shipping_cost = $order->get_total_shipping();
// set the address fields
$user_id = $order->get_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 );
}
}
// set the username and password
$api_username = 'username';
$api_password = 'password';
// setup the data which has to be sent
$data = array(
'username' => $api_username,
'password' => $api_password,
'name1' => $address['billing_first_name'],
'street' => $address['billing_address_1'],
'city' => $address['billing_city'],
'country' => $address['billing_country'],
'pcode' => $address['billing_postcode'],
'phone' => $phone,
'weight' => 4.0,
'parcel_type' => 'D',
'num_of_parcel' => 1
);
// send API request via cURL
$curl = 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($curl, CURLOPT_URL, "https://easyship.si/api/parcel/parcel_import");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec ($curl);
curl_close ($curl);
}
However, it does not pass the request, the third-party API does not receive anything. WP_DEBUG doesn't see any errors. Tell me where to look, what is wrong?
You can use https://requestbin.com/ as endpoint to see data you are posting and if they are being posted correctly. If it successfully posting it to requestbin endpoint you can see the header and body formats as well. If its not working try debugging line by line.
Sorry I wasn't able to add this as comment
Below is my code to create or update subscription in Mailchimp.
function mailchimp_ajax_subscription()
{
$data = isset( $_POST['formData'] ) ? $_POST['formData'] : array();
ob_start();
if(count($data) > 0)
{
$api_key = 'XXXXXXXXXXXXXX';
$status = 'unsubscribed'; // subscribed, unsubscribed, cleaned, pending
$args = array(
'method' => 'PUT',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
),
'body' => json_encode(array(
'email_address' => $data["email"],
'status' => $status,
'tags' => array($data["name"])
))
);
$response = wp_remote_post( 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/XXXXX/members/' . md5(strtolower($email)), $args );
$body = json_decode( $response['body'] );
if ( $response['response']['code'] == 200 && $body->status == $status ) {
echo 'The user has been successfully ' . $status . '.';
} else {
echo '<b>' . $response['response']['code'] . $body->title . ':</b> ' . $body->detail;
}
}
wp_die();
}
Using above code, I can create subscription into Mailchimp but If I enter same email to edit the tags/status it gives me error.
Error:
400Member Exists: abc#xyz.com is already a list member. Use PUT to
insert or update list members.
I already used PUT in the code so what is missing?
You can use TAGS API to update the TAGS. Below is the sample code to update the TAG:
$postdata = array(
'apikey' => $api_key,
'email_address' => $userData["email"],
'tags' => array(
array(
'name' => $userData["name"],
'status' => 'active'
),
)
);
$mch_api = curl_init(); // initialize cURL connection
curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($userData['email']))."/tags");
curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
curl_setopt($mch_api, CURLOPT_POST, true);
curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($postdata) ); // send data in json
$result = curl_exec($mch_api);
How can I use wp_remote_post to send SMS from Twilio?
The code below work nice, but it need to be done with WordPress HTTP API using wp_remote_post
function send_twilio_text_msg($id, $token, $from, $to, $body)
{
$url = "https://api.twilio.com/2010-04-01/Accounts/".$id."/SMS/Messages";
$data = array (
'From' => $from,
'To' => $to,
'Body' => $body,
);
$post = http_build_query($data);
$x = curl_init($url );
curl_setopt($x, CURLOPT_POST, true);
curl_setopt($x, CURLOPT_RETURNTRANSFER, true);
curl_setopt($x, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($x, CURLOPT_USERPWD, "$id:$token");
curl_setopt($x, CURLOPT_POSTFIELDS, $post);
$y = curl_exec($x);
curl_close($x);
print_r($y);
}
To make a call to the Twilio API using wp_remote_post you need to do a few things:
Create the URL, as you did in your example
Collect the data you want to send (your From, To, and Body from your example)
Create an Authorization header out of your Account Sid and Auth Token. To do so, you need an array with one key, Authorization and the value made from base 64 encoding the Account Sid and Auth Token concatenated with a colon.
The $url is the first argument to wp_remote_post the second argument is an associative array with body and headers properties.
See the example below:
function send_twilio_text_msg($id, $token, $from, $to, $body)
{
$url = "https://api.twilio.com/2010-04-01/Accounts/".$id."/SMS/Messages";
$data = array(
'From' => $from,
'To' => $to,
'Body' => $body
);
$headers = array(
'Authorization' => 'Basic ' . base64_encode($id . ':' . $token)
);
$result = wp_remote_post($url, array(
'body' => $data,
'headers' => $headers
));
}
how do I get my app (Android and iOS) receive push notifications, automatically from a control panel, or a webservice ... the only way I found is at the Appcelerator panel, and is not automatic ...
I wish, for example, when there was an update in the database, the push appeared in the user's mobile phone in question ... how can you do this?
Another thing is that how can I do that is generic for android and ios? Is there any module or video lesson to teach to do that?
For Multi Push Module
This is an part of my php for multi-platform push firing.
// iOS
$apnsHost = 'gateway.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-prod.pem';
//
$abrirPush = 1;
$mensagem = array(
"id_push" => $id_push,
"id_empresa" => $id_empresa,
"abrir" => $abrirPush
);
$mensagem = json_encode($mensagem);
$arr['aps'] = array(
'alert' => $nome_empresa . ' tem uma mensagem para você' ,
'badge' => 1,
'sound' => 'default',
'body' => $mensagem
);
$payload = json_encode($arr);
// Abre Conexão iOS
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
// Android
$arrayAndroid = Array();
// Search for iOS or Android Device
for ($i = 0; $i < count($montaArray); $i++){
$pegaToken = $montaArray[$i];
$contaToken = strlen($pegaToken);
if ($contaToken < 70){
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $montaArray[$i])) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
}else{
/// PUSH ANDROI ////
array_push( $arrayAndroid, $montaArray[$i]);
}
}
fclose($apns);
// API access key from Google API's Console ( Server KEY )
define( 'API_ACCESS_KEY', 'KEY_HERE' );
// prep the bundle
$msg = array
(
'message' => 'Tem uma mensagem para você',
'title' => $nome_empresa,
'payload' => $mensagem,
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $arrayAndroid,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
print $result;
curl_close( $ch );
note that Apple doesn't alloy an array of tokens like Google does. As Android's token are larger than iOS's Tokens, I count it length to know which token it is.
$montaArray comes from my database where all token are stored