Dalam kode Anda ada hal-hal aneh seperti 'order_id' => $email
itu seharusnya nilai ID Pesanan dan bukan email… Juga $customer_id= $order->id;
itu BUKAN ID pengguna pelanggan, tetapi ID Pesanan, dan $email1= $order->id;
yang tidak digunakan dan salah… */
<?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 -------------------------#
?>
Juga yang aneh adalah Anda dapat memesan banyak item (produk) dan meja Anda tidak dapat menangani ini, karena Anda juga membutuhkan baris demi item…