2017-07-24 39 views
0

這是一個幾天,現在,我似乎無法找到添加/更新用戶的元數據woocommerce管理員創建爲了得到客戶的ID上創建

add_user_meta('user_id', 'custom_key', 'custom_value'); 

創造woocommerce管理訂單時鉤(woocommerce-> orders->添加訂單),後端。 使用

add_action('woocommerce_process_shop_order_meta', 'admin_process_shop_order', 10, 1); 

這適用於訂單處理時的處理。但是,我需要獲取客戶ID,從我所能告訴的客戶ID不存在直到訂單實際創建(有意義)。

所以,我可以用什麼掛鉤(或其他解決方案),以獲得客戶ID一旦創建我的問題的順序和可搜索與

get_post_meta($order_id, '_customer_user', true); 
+0

嘗試followng鉤與訂單狀態, 'woocommerce_order_status_pending woocommerce_order_status_failed woocommerce_order_status_on按住 woocommerce_order_status_processing woocommerce_order_status_completed woocommerce_order_status_refunded woocommerce_order_status_cancelled' – Gugan

+0

鉤執行後付款完全是,'woocommerce_payment_complete' – Gugan

回答

1

謝謝@Gugan您的建議!它看起來像你的幫助,我終於能夠得到這個混亂排序:)

因爲我只想這一次發射,即當訂單創建(而不是再次更新時),我希望合併兩個行動。

第一個'woocommerce_process_shop_order_meta'。在這裏,我可以檢查後存在元(如果是這樣,訂單已經生成,應單獨留在家中)

function check_order($post_id){ 
    $new_order = get_post_meta($post_id, '_customer_user', true); 
    if(!$new_order){ 
     add_action('woocommerce_order_status_[MY_CUSTOM_ORDER_STATUS]-processing', 'total_count'); 
    } 
}add_action('woocommerce_process_shop_order_meta', 'check_order', 10, 1); 

如果這是上「woocommerce_order_status_ [MY_CUSTOM_ORDER_STATUS] - 處理新秩序的舉動「(我的功能‘TOTAL_COUNT’

function total_count($post_id){ 
    $order = wc_get_order($post_id); 
    $customer_id = $order->get_user_id(); 
    $user_role = get_user_meta($customer_id, 'wp_capabilities', true); 
    $custom = serialize(array('[MY_CUSTOM_USER_ROLE]' => true)); 
    $today = date('Y-m-d'); 
    if($user_role = $custom){ 
     $current_total = get_user_meta($customer_id, 'total', true); 
     $increment_total = $current_total+1; 
     update_user_meta($customer_id, 'total', $increment_total); 
     update_user_meta($customer_id, 'last', $today); 
    } 
} 

現在我只得到我的自定義用戶METAS‘總’和增量‘最後’如果這是一個新的秩序,如果客戶是我的自定義用戶角色。另外,它只適用於一個訂單狀態(即在我的情況下,[MY_CUSTOM_ORDER_STATUS] - 處理)。

只要記下我的解決方案,在這裏尋找處理類似的自定義訂單創建工作的任何人。

相關問題