我试图添加一个短信api到Wordpress,它使用WooCommerce钩子发送订单确认消息。经过一番研究,我在这里找到了下面的代码,它的工作原理是一样的。
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>
请告诉我如何将此API集成到上述Wordpress脚本中。
据推测,您正在尝试将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文件,存储在你插件的某个地方。它利用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; });