【问题标题】:Insert Values into a custom table once order is placed in Woocommerce在 Woocommerce 中下订单后,将值插入自定义表中
【发布时间】:2017-01-23 18:03:15
【问题描述】:

我需要插入名为 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',
    ) );

   }

【问题讨论】:

    标签: php mysql wordpress woocommerce hook-woocommerce


    【解决方案1】:

    要真正帮助您(并测试您的真实代码),您应该提供用于创建表(或 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 -------------------------#
    
    ?>
    

    另外奇怪的是,您可以在一个订单中有很多商品(产品),而您的桌子无法处理这个,因为您还需要逐个商品......

    【讨论】:

      猜你喜欢
      • 2016-05-07
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 2019-01-23
      • 2021-01-04
      • 2021-01-28
      • 2018-02-06
      • 2021-06-10
      相关资源
      最近更新 更多