我使用下面的API代码发送短信当一个新的订单时,SMS API代码正在工作发送SMS .放置在functions.php文件的子主题末尾.所有更新(WordPress 5.9.3和6.4与PHP8.0)
2期:
variables.
试过以下几点:
文档:https://www.textlocal.in/free-developer-sms-api/
有任何建议来修改代码以便解决这两个问题吗?
谢谢
// Sending SMS to customers on new orders
add_action('woocommerce_new_order', 'custom_msg_customer_process_order', 10, 3);
function custom_msg_customer_process_order ($order_id) {
$order = new WC_Order( $order_id );
$order_date = $order->get_date_created();
$billing_phone = $order->get_billing_phone();
$apiKey = urlencode('apikey');
// Message details
$numbers = array($billing_phone,91xxxxxxxxxx);
$sender = urlencode('TXTCL');
$message = rawurlencode('Thank you for buying from us, a Wellness product. Your order number $order_id Dated $order_date is confirmed.');
$numbers = implode(',', $numbers);
// Prepare data for POST request
$data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender" => $sender, "message" => $message);
// Send the POST request with cURL
$ch = curl_init('https://api.textlocal.in/send/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Process your response here
echo $response;
}发布于 2022-04-15 13:37:51
虽然我对文本API不太了解(我当然希望您的客户选择使用文本更新!),但我认为new WC_Order从未从数据库中读取您的数据。如何获得一个新的order对象是wc_get_order()。但是,woocommerce_new_order钩子沿$order object as the 2nd parameter传递。
因此,如果我们确保回调需要第二个参数,就不需要重新实例化order对象。
至于第2部分,当订单保存到DB时,woocommerce_new_order就会触发,而订单状态是什么并不重要。相反,我认为我们可以使用woocommerce_order_status_pending_to_processing,这是新订单电子邮件使用的方式。
/**
* Sending SMS to customers on new orders.
*
* @param int $order_id The order ID. *
* @param WC_Order $order Order object.
*/
function custom_msg_customer_process_order( $order_id, $order ) {
$order_date = $order->get_date_created();
$billing_phone = $order->get_billing_phone();
$apiKey = urlencode('apikey');
// Message details
$numbers = array($billing_phone,91xxxxxxxxxx);
$sender = urlencode('TXTCL');
// Use sprintf() to replace placeholders with values.
$message = rawurlencode( sprintf( 'Thank you for buying from us, a Wellness product. Your order number %s Dated %s is confirmed.', $order_id, $order_date ) );
$numbers = implode(',', $numbers);
// Prepare data for POST request
$data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender" => $sender, "message" => $message);
// Send the POST request with cURL
$ch = curl_init('https://api.textlocal.in/send/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Process your response here
echo $response;
}
add_action('woocommerce_order_status_pending_to_processing', 'custom_msg_customer_process_order', 10, 2 );替代
以上情况取决于您的网关将订单设置为processing。如果它没有(或者不是最初的pending),那么它可能不会开火.因为它只在从pending到processing的转换中触发。一个更好的钩子可能实际上是woocommerce_payment_complete。但请注意,在源中,它只向其回调传递一个参数.$order_id (类似于woocommerce_thankyou钩子)。因此,需要对代码片段进行调整,以便只需要一个参数:
/**
* Sending SMS to customers on new orders.
*
* @param int $order_id The order ID. *
*/
function custom_msg_customer_process_order( $order_id ) {
$order = wc_get_order( $order_id );
// Quit early if not a valid order.
if ( ! $order ) {
return;
}
$order_date = wc_format_datetime( $order->get_date_created() );
$billing_phone = $order->get_billing_phone();
$apiKey = urlencode('apikey');
// Message details
$numbers = array($billing_phone,91xxxxxxxxxx);
$sender = urlencode('TXTCL');
// Use sprintf() to replace placeholders with values.
$message = rawurlencode( sprintf( 'Thank you for buying from us, a Wellness product. Your order number %s Dated %s is confirmed.', $order_id, $order_date ) ) );
$numbers = implode(',', $numbers);
// Prepare data for POST request
$data = array('apikey' => $apiKey, 'numbers' => $numbers, "sender" => $sender, "message" => $message);
// Send the POST request with cURL
$ch = curl_init('https://api.textlocal.in/send/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Process your response here
echo $response;
}
add_action('woocommerce_payment_complete', 'custom_msg_customer_process_order' );https://stackoverflow.com/questions/71881179
复制相似问题