在woocommerce thankyou.php文件中,有两个行动
<?php do_action( 'woocommerce_thankyou_' . $order->payment_method, $order->id ); ?>
<?php do_action( 'woocommerce_thankyou', $order->id ); ?>
在我的代码中,我在functions.php文件中添加了一个动作
add_action( 'woocommerce_thankyou', 'custom_woocommerce_complete_order_sms' );
function custom_woocommerce_complete_order_sms( $order_id ) {
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$product_list = '';
$order_item = $order->get_items();
foreach( $order_item as $product ) {
$prodct_name[] = $product['name']."x".$product['qty'];
}
$product_list = implode( ',', $prodct_name );
$order_amount = get_post_meta( $order_id, '_order_total', true );
// Step 1: Download the Twilio-PHP library from twilio.com/docs/libraries,
// and move it into the folder containing this file.
require "twilio-php-master/Services/Twilio.php";
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "xxxx";
$AuthToken = "xxx";
// Step 3: instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);
// Step 4: make an array of people we know, to send them a message.
// Feel free to change/add your own phone number and name here.
$name = "Test";
$number = "xxxx";
// $number is a phone number above, and
// $name is the name next to it
$sms = $client->account->messages->sendMessage(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased, or the (deprecated) Sandbox number
"+44xxxxxx",
// the number we are sending to - Any phone number
$number,
// the sms body
"Hey $name, there is a new Order, the order is, $product_list, the order ID is $order_id and the total amount payable is $order_amount"
);
// Display a confirmation message on the screen
//echo "Sent message to $name";
}
这会在订单上发送消息,消息会在很多时间内发送两次,这是否因为两个感谢你在woocommerce模板文件中的行为?
试图找到关于该文件的一些文档,但实际上找不到多少。
不确定是否可以安全地删除一个以及删除哪一个,任何人都可以指出我正确的方向
也奇怪它是如何不会一直发生的,我想另一个选择是写日志并看看为什么要这么做?
答案 0 :(得分:2)
这个具有欺骗性,因为它们实际上是两个完全不同的钩子。请注意,一个以下划线字符结尾,然后是一个点连接器和持有付款方式的变量。此挂钩是动态的,会解析为woocommerce_thankyou_stripe
或woocommerce_thankyou_paypal
等等。
我不完全确定原因,但WooCommerce有时会在一次结帐时多次加载thankyou.php模板。解决此问题的最佳方法可能是设置会话变量以检查您是否已发送短信。我已更新您的代码以包含此检查:
add_action( 'woocommerce_thankyou', 'custom_woocommerce_complete_order_sms' );
function custom_woocommerce_complete_order_sms( $order_id ) {
// Check for the existence of a session, and start one if it's not been started yet
// Note that session_status() is only available in PHP 5.4.0+
if ( session_status() == PHP_SESSION_NONE ) {
session_start();
}
// Check for the existnece of a session variable indicating we've sent the SMS, which we set at the end
// Abort the function if we've already sent the SMS
if (isset($_SESSION['is_sms_sent'])) {
return;
}
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$product_list = '';
$order_item = $order->get_items();
foreach( $order_item as $product ) {
$prodct_name[] = $product['name']."x".$product['qty'];
}
$product_list = implode( ',', $prodct_name );
$order_amount = get_post_meta( $order_id, '_order_total', true );
// Step 1: Download the Twilio-PHP library from twilio.com/docs/libraries,
// and move it into the folder containing this file.
require "twilio-php-master/Services/Twilio.php";
// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "xxxx";
$AuthToken = "xxx";
// Step 3: instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);
// Step 4: make an array of people we know, to send them a message.
// Feel free to change/add your own phone number and name here.
$name = "Test";
$number = "xxxx";
// $number is a phone number above, and
// $name is the name next to it
$sms = $client->account->messages->sendMessage(
// Step 6: Change the 'From' number below to be a valid Twilio number
// that you've purchased, or the (deprecated) Sandbox number
"+44xxxxxx",
// the number we are sending to - Any phone number
$number,
// the sms body
"Hey $name, there is a new Order, the order is, $product_list, the order ID is $order_id and the total amount payable is $order_amount"
);
// Set our session variable after we've sent the SMS
$_SESSION['is_sms_sent'] = true;
// Display a confirmation message on the screen
//echo "Sent message to $name";
}