Strange
Strange

Reputation: 308

Save WooCommerce order to remote database

I have hosted a remote MySQL database with a table order_master and configured a database connection in config.php and it successfully connected to the remote database. On my Localhost I have a WooCommerce store, now whenever an order is placed in WooCommerce store I want it to also get saved in remote MySQL database order_master.

I have tried using woocommerce_order_status_completed but its not working. I added the below code in functions.php file:

add_action('woocommerce_order_status_completed','payment_complete');
function payment_complete($order_id)
{
    alert('Function Called..'); //to check, but function not called
   global $items;
   $order = new WC_Order($order_id);
   global $woocommerce;
   global $new_wpdb;
    $total_amt=WC()->cart->cart_contents_total;
    $stmt = "INSERT INTO order_master (payment_amt) VALUES (%d)";
    $new_wpdb->query( $stmt,$total_amt);
}

Can anyone please tell me how to do that, I am not able to get any hint for it.

Upvotes: 1

Views: 1022

Answers (1)

user10493250
user10493250

Reputation:

Use woocommerce_new_order hook to save any new order received. Try this method and tell me if it worked.

    add_action( 'woocommerce_new_order', 'your_order_details',  1, 1  );
        function your_order_details($order_id){
/* Configure your remote DB settings here */
        $host = 'localhost';
        $user = 'root';
        $pass = '';
        $db = 'database';
        $new_wpdb = mysqli_connect($host, $user, $pass, $db);
        if (!$new_wpdb) {
            echo mysqli_error($new_wpdb);
        }
        $order = new WC_Order($order_id);
        $order_num = $order_id;         // Order ID
        $order_amount = get_post_meta($order_id, '_order_total', true);     // Amount
        $order_master = "INSERT INTO order_master (payment_amt) VALUES ('$order_amount')";
        $order_master_exe = mysqli_query($new_wpdb, $order_master);
        if ($order_master) {
            mysqli_close($new_wpdb);
        }

Upvotes: 2

Related Questions