一旦在Woocommerce中下了订单,就将Values插入到自定义表中 [英] Insert Values into a custom table once order is placed in Woocommerce

查看:217
本文介绍了一旦在Woocommerce中下了订单,就将Values插入到自定义表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在名为 license_table

 **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',
    ) );

   }

推荐答案

要真正帮助您(并测试真实的代码),您应该提供用于创建表(或sql查询)的代码,以更新您的问题.

To really help you (and testing for real your code), you should provide the code that you are using to create your table (or the sql query) updating your question.

在您的代码中,有一个奇怪的东西,例如 'order_id' => $email ,应该是订单ID值,而不是电子邮件……还有 $customer_id= $order->id; ,它不是ID客户用户,但是订单ID和未使用的 $email1= $order->id; ,这是错误的…*/

In your code there is strange things as 'order_id' => $email that should be the Order ID value and not the email… Also $customer_id= $order->id; that is NOT the ID of the customer user, but the Order ID, and $email1= $order->id; that is not used and it's wrong… */

<?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 -------------------------#

?>

还有一个奇怪的是,您可以在一个订单中有很多物品(产品),而您的表无法处理此事情,因为您还需要逐行…

Also what is strange is that you can have many items (products) in an order and your table can't handle this, as you should need also a line by item…

这篇关于一旦在Woocommerce中下了订单,就将Values插入到自定义表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆