Insert values into a custom table once order is placed in Woocommerce

In the following tutorial i was challenged by my client that to Insert values into a custom table once order is placed in Woocommerce WordPress database’s so that we can use it for new ui in codeigniter backend.

We were running LMS on client site but client needed new backend panel as the users are so dumb to understand the WordPress front end, so i have to create a new backend panel for them to use, where they can add their data.
The biggest challenge here is they want woocommerce functionality and they want custom table where the username, password, email and some LMS(Learning Management Sytem) functionality like days, purchase date and current day etc., so these fields are added for them accordingly.

So lets start the simplest coding for a complex idea. It was really easy for me who use WordPress for almost all clients website with out any hassle. The method to follow in this tutorial was simple, we need to create a function after the woocommerce’s order status to be completed. So i kept let of reading through hooks, functions and filters and created a simple function, when order status completed then insert the values in custom table of WordPress database.

I created a table name vusers in WordPress database and added the fields i want accordingly. You can create your fields from this demo.
You need to add the simple snippet in themes/themename/functions.php file.


add_action( 'woocommerce_order_status_completed', 'insertforci_function' );
function insertforci_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;
    $password = $user_data->user_pass;
    $user_id = $user_data->ID;

    $thedate = date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );

    $thedate_new = date('Y-m-d', strtotime("+5 months", strtotime($thedate)));


    $days = 150;
    $which_day = 1;
    // "$wpdb->prefix" will prepend your table prefix
    $table_name = "vusers";

    // This array is for adding the values in database
    $data = array( 
        'username'          => $customer_login_name,
        'password'          => $password,
        'order_id'          => $order_id,
        'loginemail'          => $customer_login_email,
        'full_name'          => $complete_billing_name,
        'start_date'          => $thedate,
        'expiry_date'          => $thedate_new,
        'days'          => $days,
        'currentday'          => $which_day,
    );

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

}

What i did in the above snippet is i created a function, where it executes only when woocommerce status of an order is completed. In that function, the order id will be passed, from that order_id we will get all the order related details, like customer username, customer email, customer password etc., with that information we create appropriate variables and add it in to database with $wpdb->insert.

After the values are inserted, i will use them in codeigniter application accordingly.

Leave a Reply