2016-09-15 58 views
2

我需要插入到我的自定義表稱爲license_table值插入自定義表一旦訂單被放置在Woocommerce

**username**, **order id**, **Quantity** 
This needs to be populated when an order is placed. 
Username = customer's email id 
Quantity = quantity (of the product) 
order id=Order ID 

我已經使用但不能正常工作

add_action('woocommerce_order_status_completed', 'my_function'); 
function my_function($order_id) { 
    global $wpdb; 
    $order = new WC_order($order_id); 
    $customer_id= $order->id; 
    $email= $order->billing_email; 
    $email1= $order->id; 
    $table_name = "aitoe_license_table"; 
    $wpdb->insert($table_name, array(
     'username' => $customer_id, 
     'order_id' => $email, 
     'number_of_cameras' => 12, 
     'boolean' => 'False', 
    )); 

    } 

回答

2

要真正幫助你(並測試真實的代碼),你應該提供你用來創建你的表(或SQL查詢)更新你的問題的代碼。

在你的代碼中有奇怪的東西'order_id' => $email應該是訂單ID值,而不是電子郵件......此外$customer_id= $order->id;是不是客戶的用戶的ID,但訂單ID,並$email1= $order->id;沒有被使用,這是錯的... */

<?php 

#-------------------- code begins below -------------------------# 

add_action('woocommerce_order_status_completed', 'my_function'); 
function my_function($order_id) { 
    global $wpdb; 

    // Getting the order (object type) 
    $order = wc_get_order($order_id); 

    // Getting order items 
    $items = $order->get_items(); 
    $total_items_qty = 0; 

    // Iterating through each item (here we do it on first only) 
    foreach ($items as $item) { 
     $total_items_qty += $item["qty"]; 
    } 

    // Here are the correct way to get some values: 
    $customer_id   = $order->customer_user; 
    $billing_email   = $order->billing_email; 
    $complete_billing_name = $order->billing_first_name . ' ' . $order->billing_last_name; 

    // Getting the user data (if needed) 
    $user_data    = get_userdata($customer_id); 
    $customer_login_name = $user_data->user_login; 
    $customer_login_email = $user_data->user_email; 

    // "$wpdb->prefix" will prepend your table prefix 
    $table_name = $wpdb->prefix."license_table"; 

    // This array is not correct (as explained above) 
    $data = array( 
     'username'   => $customer_login_name, 
     'order_id'   => $order_id, 
     'number_of_cameras' => $total_items_qty, 
     'boolean'   => 'false', 
    ); 

    $wpdb->insert($table_name, $data); 

} 

#-------------------- code end -------------------------# 

?> 

而且令人奇怪的是,你可以在一個訂單很多項目(產品)和你的表不能處理這個問題,你應該還需要人一個項目...

相關問題