我编写了一个 WooCommerce 插件,用于在订单编辑页面中通过短信为客户的 billing_phone 发送跟踪代码。
发送公司(例如 Post、Tipax、FedEx 等)发送包裹后,会从他们那里收到跟踪代码。我想通过短信将该代码发送给客户。
<?php
/*
Plugin Name: Order Tracking Plugin
Plugin URI: #
Description: A plugin for sending order tracking codes via SMS.
Version: 1.0
Author: Paul Vek
*/
// Add order tracking fields after shipping address
add_action('woocommerce_admin_order_data_after_shipping_address', 'display_order_tracking_fields');
function display_order_tracking_fields($order) {
// Retrieve order and customer information
$order_id = $order->get_id();
$billing_first_name = $order->get_billing_first_name();
$billing_last_name = $order->get_billing_last_name();
$customer_phone = $order->get_billing_phone();
$order_number = $order->get_order_number();
$tracking_code = get_post_meta($order_id, 'tracking_code', true);
// Display order tracking fields
?>
<div class="order_tracking_container">
<h3><?php esc_html_e('Order Tracking', 'your-textdomain'); ?></h3>
<p>
<label for="tracking_code"><?php esc_html_e('Tracking Code:', 'your-textdomain'); ?></label>
<input type="text" id="tracking_code" name="tracking_code" value="<?php echo esc_attr($tracking_code); ?>">
</p>
<p>
<label for="sending_method"><?php esc_html_e('Sending Method:', 'your-textdomain'); ?></label>
<select id="sending_method" name="sending_method">
<option value="tipax">Tipax</option>
<option value="post">Post</option>
<option value="chapar">Chapar</option>
</select>
</p>
<p>
<label for="tracking_address"><?php esc_html_e('Tracking Address:', 'your-textdomain'); ?></label>
<select id="tracking_address" name="tracking_address">
<option value="trcking.post.ir">trcking.post.ir</option>
<option value="tipaxco.com">tipaxco.com</option>
</select>
</p>
<p>
<button id="send_tracking_code" class="button"><?php esc_html_e('Send Tracking Code', 'your-textdomain'); ?></button>
</p>
<div id="tracking_code_message"></div>
</div>
<script>
jQuery(document).ready(function($) {
$('#send_tracking_code').click(function() {
var tracking_code = $('#tracking_code').val();
var sending_method = $('#sending_method').val();
var tracking_address = $('#tracking_address').val();
var order_id = $('input[name="post_ID"]').val();
$.ajax({
type: 'POST',
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: {
action: 'send_tracking_code',
tracking_code: tracking_code,
sending_method: sending_method,
tracking_address: tracking_address,
order_id: order_id
},
success: function(response) {
$('#tracking_code_message').html(response);
},
error: function(xhr, status, error) {
console.log(xhr.responseText);
}
});
});
});
</script>
<?php
}
// AJAX handler for sending tracking code
add_action('wp_ajax_send_tracking_code', 'send_tracking_code');
function send_tracking_code() {
// Check if the request is coming from an AJAX call
if (!defined('DOING_AJAX') || !DOING_AJAX) {
wp_die();
}
// Get the order ID and tracking details from the AJAX request
$order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : 0;
$tracking_code = isset($_POST['tracking_code']) ? sanitize_text_field($_POST['tracking_code']) : '';
$sending_method = isset($_POST['sending_method']) ? sanitize_text_field($_POST['sending_method']) : '';
$tracking_address = isset($_POST['tracking_address']) ? sanitize_text_field($_POST['tracking_address']) : '';
// Check if all required data is present
if (empty($order_id) || empty($tracking_code) || empty($sending_method) || empty($tracking_address)) {
wp_send_json_error(__('Missing required parameters.', 'order-tracking-plugin'));
}
// Compose message
$order = wc_get_order($order_id);
$billing_first_name = $order->get_billing_first_name();
$billing_last_name = $order->get_billing_last_name();
$order_id = $order->get_order_id();
$message = sprintf(__('Hello %s %s dear. The order %s was sent through %s. Tracking code: %s. To track it, go to this address: %s.', 'order-tracking-plugin'), $billing_first_name, $billing_last_name, $order_id, $sending_method, $tracking_code, $tracking_address);
// Send SMS using API
$api_key = '';
$sender_number = '3000505';
$username = '';
$password = '';
$rcpt_nm = array($billing_phone); // Use customer's phone number as recipient
$url = "https://ippanel.com/services.jspd";
$params = array(
'uname' => $username,
'pass' => $password,
'from' => $sender_number,
'message' => $message,
'to' => json_encode($rcpt_nm), // Use rcpt_nm as recipients
'op' => 'send'
);
$handler = curl_init($url);
curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handler, CURLOPT_POSTFIELDS, $params);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handler);
$response = json_decode($response);
// Check if SMS was sent successfully
if ($response[0] != 200) {
wp_send_json_error(__('Failed to send tracking code.', 'order-tracking-plugin'));
}
// Prevent order from being saved during AJAX request
add_filter('woocommerce_before_order_object_save', 'disable_order_save_during_ajax');
// Return success message
wp_send_json_success(__('Tracking code sent successfully.', 'order-tracking-plugin'));
}
字段显示正确
但是我的代码有两个问题
不是通过Ajax发送并显示发送的结果,例如在同一页面以消息的形式,更新页面,更新订单!如果我不想进行任何更新,也不希望对数据库进行任何更改。只需发送代码。
短信根本没有发送,我的插件也不起作用
这是我的网络服务FarazSms/ippanel(一家短信服务公司)用于发送短信的示例代码。 我在我的插件中正确使用了这个方法吗?
<?php
$url = "https://ippanel.com/services.jspd";
$rcpt_nm = array('9121111111','9122222222');
$param = array
(
'uname'=>'',
'pass'=>'',
'from'=>'',
'message'=>'test',
'to'=>json_encode($rcpt_nm),
'op'=>'send'
);
$handler = curl_init($url);
curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handler, CURLOPT_POSTFIELDS, $param);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
$response2 = curl_exec($handler);
$response2 = json_decode($response2);
$res_code = $response2[0];
$res_data = $response2[1];
echo $res_data;
?>
您还可以在此链接查看更多详细信息。
您的第一个函数中缺少一些内容,以避免您面临的问题(避免通过重新加载进行订单更新)。
此外,JavaScript 代码最好放在页脚中。
将您的第一个函数替换为以下内容:
// Add order tracking fields after shipping address
add_action('woocommerce_admin_order_data_after_shipping_address', 'display_order_tracking_fields');
function display_order_tracking_fields($order) {
// Retrieve order and customer information
$order_id = $order->get_id();
$billing_first_name = $order->get_billing_first_name();
$billing_last_name = $order->get_billing_last_name();
$customer_phone = $order->get_billing_phone();
$order_number = $order->get_order_number();
$tracking_code = get_post_meta($order_id, 'tracking_code', true);
// Display order tracking fields
?>
<div class="order_tracking_container">
<h3><?php esc_html_e('Order Tracking', 'your-textdomain'); ?></h3>
<p>
<label for="tracking_code"><?php esc_html_e('Tracking Code:', 'your-textdomain'); ?></label>
<input type="text" id="tracking_code" name="tracking_code" value="<?php echo esc_attr($tracking_code); ?>">
</p>
<p>
<label for="sending_method"><?php esc_html_e('Sending Method:', 'your-textdomain'); ?></label>
<select id="sending_method" name="sending_method">
<option value="tipax">Tipax</option>
<option value="post">Post</option>
<option value="chapar">Chapar</option>
</select>
</p>
<p>
<label for="tracking_address"><?php esc_html_e('Tracking Address:', 'your-textdomain'); ?></label>
<select id="tracking_address" name="tracking_address">
<option value="trcking.post.ir">trcking.post.ir</option>
<option value="tipaxco.com">tipaxco.com</option>
</select>
</p>
<p>
<button type="button" id="send_tracking_code" class="button"><?php esc_html_e('Send Tracking Code', 'your-textdomain'); ?></button>
</p>
<div id="tracking_code_message"></div>
</div>
<?php
}
// Jquery code
add_action( 'admin_footer', 'wc_order_admin_footer_script' );
function wc_order_admin_footer_script() {
global $pagenow, $typenow;
if( $pagenow === 'post.php' && $typenow === 'shop_order' ) :
?>
<script>
jQuery( function($) {
$('#send_tracking_code').click(function(e) {
e.preventDefault();
var tracking_code = $('#tracking_code').val();
var sending_method = $('#sending_method').val();
var tracking_address = $('#tracking_address').val();
var order_id = $('input[name="post_ID"]').val();
$.ajax({
type: 'POST',
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: {
action: 'send_tracking_code',
tracking_code: tracking_code,
sending_method: sending_method,
tracking_address: tracking_address,
order_id: order_id
},
success: function(response) {
$('#tracking_code_message').html(response);
},
error: function(xhr, status, error) {
console.log(xhr.responseText);
}
});
});
});
</script>
<?php
endif;
}
代码位于子主题的functions.php 文件中(或插件中)。应该可以解决问题。
代码更改:
type="button"
。preventDefault()
添加到您的 jQuery 单击事件中。admin_footer
操作挂钩中的函数将其添加到页脚中。感谢@LoicTheAztec 修复了第一个功能。
现在,要修复第二个功能并修复不发送短信的问题,只需使用以下代码即可。已测试并工作。
// AJAX handler for sending tracking code
add_action('wp_ajax_send_tracking_code', 'send_tracking_code');
function send_tracking_code() {
// Check if the request is coming from an AJAX call
if (!defined('DOING_AJAX') || !DOING_AJAX) {
wp_die();
}
// Get the order ID and tracking details from the AJAX request
$order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : 0;
$tracking_code = isset($_POST['tracking_code']) ? sanitize_text_field($_POST['tracking_code']) : '';
$sending_method = isset($_POST['sending_method']) ? sanitize_text_field($_POST['sending_method']) : '';
$tracking_address = isset($_POST['tracking_address']) ? sanitize_text_field($_POST['tracking_address']) : '';
// Check if all required data is present
if (empty($order_id) || empty($tracking_code) || empty($sending_method) || empty($tracking_address)) {
wp_send_json_error(__('Missing required parameters.', 'order-tracking-plugin'));
}
// Compose message
$order = wc_get_order($order_id);
$billing_first_name = $order->get_billing_first_name();
$billing_last_name = $order->get_billing_last_name();
$billing_phone = $order->get_billing_phone(); // Retrieve billing phone
$order_id = $order->get_order_number();
$message = sprintf(__('Hello %s %s dear. The order %s was sent through %s. Tracking code: %s. To track it, go to this address: %s.', 'order-tracking-plugin'), $billing_first_name, $billing_last_name, $order_id, $sending_method, $tracking_code, $tracking_address);
// Send SMS using API
$api_key = '';
$sender_number = '3000505';
$username = '';
$password = '';
$rcpt_nm = array($billing_phone); // Use customer's phone number as recipient
$url = "https://ippanel.com/services.jspd";
$params = array(
'uname' => $username,
'pass' => $password,
'from' => $sender_number,
'message' => $message,
'to' => json_encode($rcpt_nm), // Use rcpt_nm as recipients
'op' => 'send'
);
$handler = curl_init($url);
curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handler, CURLOPT_POSTFIELDS, $params);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handler);
$response = json_decode($response);
// Check if SMS was sent successfully
if ($response[0] != 200) {
wp_send_json_error(__('Failed to send tracking code.', 'order-tracking-plugin'));
}
// Return success message
wp_send_json_success(__('Tracking code sent successfully.', 'order-tracking-plugin'));
}
我忘记使用这个
$billing_phone = $order->get_billing_phone(); // Retrieve billing phone
现在我在 // Compose message 部分下面添加了。
我将
$order_id = $order->get_order_id();
更改为$order_id = $order->get_order_number();
我从代码中删除了
// Prevent order from being saved during AJAX request add_filter('woocommerce_before_order_object_save', 'disable_order_save_during_ajax');
。