<?php
/**
* Template Name: SMS
*/
?>
<?php
$order_id = 5689;
$order = new WC_Order( $order_id );

smsprocessing($order_id, $order, "processing");

function smsprocessing($order_id, $order, $order_status){	
	$type = "xml";
	$id = get_field('outreach_id', 'option');
	$pass = get_field('outreach_password', 'option');
	$lang = "English";
	$mask = "VEGA PK";

	$firstName = empty($order->get_shipping_first_name()) ? $order->get_billing_first_name() : $order->get_shipping_first_name();
	$lastName = empty($order->get_shipping_last_name()) ? $order->get_billing_last_name() : $order->get_shipping_last_name();

	$client_name = $firstName. " ".$lastName;
	$to = "92".substr($order->get_billing_phone(), -10);
	if ($order_status == "processing"){
		$message = get_field('processing_message', 'option');
	}elseif ($order_status == "shipped"){
		$message = get_field('shipped_message', 'option');
	}elseif ($order_status == "delivered"){
		$message = get_field('delivered_message', 'option');
	}
	$message = str_replace("{name}", $client_name, $message);
	$message = str_replace("{order_id}", $order_id, $message);
	$message = urlencode($message);

	$data = "id=".$id."&pass=".$pass."&msg=".$message."&to=".$to."&lang=".$lang."&mask=".$mask."&type=".$type;
	$ch = curl_init('http://www.outreach.pk/api/sendsms.php/sendsms/url');
	curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$result = curl_exec($ch); //This is the result from Outreach
	curl_close($ch);

	$note = "SMS sent to customer for order ".$order_status ;
	$order->add_order_note( $note );
}
?>