我正在尝试向Wordpress添加短信api,它使用Woocommerce挂钩发送订单确认消息。经过一些研究,我发现以下代码here的工作方式相同。
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
//Lets get data about the order made
$order = new WC_Order( $order_id );
//Now will fetch customer/buyer id here
$customer_id = $order->user_id;
//now finally we fetch phone number
$billing_phone = get_user_meta( $customer_id, 'billing_phone', true );
// Now put your HTTP SMS API URL . I PUT WHICH WE ARE USING
$jsonurl = "http://tsms.thirdeyegoa.com/api/sendmsg.php?user=USERNAME&pass=PASSWORD&sender=MYSENDERID&phone=".$billing_phone."&priority=ndnd&stype=normal&text=MY MESSAGE TO CUSTOMER.";
// NOW WILL CALL FUNCTION CURL
$json = curl($jsonurl);
return $order_id;
}
我的sms网关提供的Api代码是
// Include provided Java Script
<script language="javascript" src="https://domainapi.js" type="text/javascript"> </script>
<script language="javascript">
// Replace your API key at below line
var apikey = 'ABCDEFGH1234567890abcdefghQWERTY123=';
// Form your data object
var mail_details = { email : 'John.Doe@foo.com', msgid : '82', listname : '', prefix : '', firstname : 'John', middlename : '', lastname : 'Doe', telephone : '', address : '', city : '', state : '', pincode : '', country : '', mobile : '9999999999', designation : '', company : '', companyphone : '', birthdate : '', anniversary : '', extra1 : '', extra2 : '' }
call_api(apikey, 'sendSingleSMS', mail_details, function(response) { document.getElementById('show').innerHTML=response; });</script>
请告诉我如何在Wordpress的上述脚本中集成此API。
答案 0 :(得分:0)
据推测,您正在尝试将API的脚本混合到“WordPress方式”中,并从WooCommerce的订单中加载一些数据。首先,您需要在主插件文件中注册脚本:
add_action( 'wp_enqueue_scripts', 'so_38554614_enqueue_scripts' );
function so_38554614_enqueue_scripts(){
wp_register_script( 'your-api', 'https://domainapi.js', array(), '1.0', true );
wp_register_script( 'your-script', 'path-to-your-script.js', array('your-api'), '1.0', true );
}
然后您需要在付款完成页面上加载它们。您还希望利用wp_localize_script()
将一些变量传递给脚本。
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
//Lets get data about the order made
$order = new WC_Order( $order_id );
wp_enqueue_script('your-api');
wp_enqueue_script('your-script');
$l18n = array( 'mail_details'=>
array(
email' => $order->billing_email,
'msgid' => 82,
'listname' => '',
'firstname' => $order->billing_first_name,
'middlename' => '',
'lastname' => $order->billing_last_name,
'telephone' => $order->billing_phone,
'address'= >$order->billing_address_1 . ' ' . $order->billing_address_2,
'city' => $order->billing_city,
'state' => $order->billing_state,
'pincode' => '',
'country' => $order->billing_country,
'mobile' => $order->billing_phone
'designation' => '',
'company' => $order->billing_company,
'companyphone' => '',
'birthdate' => '',
'anniversary' => '',
'extra1' => '',
'extra2' => ''
),
'apikey' => 'ABCDEFGH1234567890abcdefghQWERTY123=' );
wp_localize_script( 'your-script', 'Your_JS_Object', $l18n );
wp_localize_script()
return $order_id;
}
最后,您的javascript文件存储在插件中的某个位置。它利用了由Your_JS_Object
:
wp_localize_script()
// Java Script path-to-your-script.js
call_api( Your_JS_Object.apikey, 'sendSingleSMS', Your_JS_Object.mail_details, function(response) { document.getElementById('show').innerHTML=response; });