我對 WooCommerce 有一個奇怪的問題。
我有一個我開發的插件,可以將來自 WooCommerce 訂單的資料發送到 HubSpot。每當下新訂單或更新現有訂單時,插件都需要觸發。我一直在使用以下截斷的代碼來嘗試實作這一目標:
add_action('save_post', 'printout', 10, 3);
function printout($post_ID, $post, $update)
{
if ("shop_order" == $post->post_type) {
$msg = $post_ID;
delegator($msg);
}
}
function delegator($order_id){
$order = get_woocommerce_order($order_id);
// assigns the actual data of the order to the $data variable
$data = get_woocommerce_order_data($order);
// assign User e-mail for search
$email = $order->get_billing_email();
//$email = get_post_meta( $order_id, '_billing_email', true );
}
我遇到的問題是,在下達新訂單后,WordPress 無法獲取訂單詳細資訊,特別是電子郵件(它回傳 null)。鉤子確實回傳了一個訂單號,所以我認為不是鉤子在做這個,而是別的。
奇怪的是,如果在下訂單后我進入訂單頁面并點擊訂單更新,它就可以作業!
所以我想知道我錯過了什么/這里出了什么問題。在 WooCommerce 可以進行資料庫呼叫之前,鉤子的流程是否會觸發?
謝謝你的幫助。快把我逼瘋了!
uj5u.com熱心網友回復:
對于通過管理員添加/更新的訂單,請使用以下代碼
add_action('save_post_shop_order', 'backend_delegator', 10, 3);
function backend_delegator($post_id, $post, $update) {
// Orders in backend only
if (!is_admin())
return;
// Get an instance of the WC_Order
$order = new WC_Order($post_id);
//Fired when create a new order
if (!$update) {
$email = $order->get_billing_email();
}
if ($update) {
$email = $order->get_billing_email();
}
}
對于從結帳下的訂單,請使用以下代碼。
add_action('woocommerce_new_order', 'frondend_delegator', 10, 2);
function frondend_delegator($order_id, $order) {
$email = $order->get_billing_email();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/461796.html
標籤:php WordPress woocommerce 钩woocommerce
