I am trying to pull the data from a completed checkout in the functions.php file of woocommerce. After checkout, I am trying to store a custom generated code, the email that generated the order, and the total amount of the order. I am unable to pull any data from the order, and have been unsuccesful in finding any documentation relating to it.
The data must be pulled after checkout is completed successfully.
The table has columns voucher code(the unique generated code per transaction), user email, and total price of order. I cannot find a way to grab the user email and order total after checkout. The query must insert the voucher ID which is generated within the function, the email which should be grabbed from the order details, redeemed which is set as 0, and the value which should also be grabbed from the order details.
On Woocommerce checkout page I have an internal server error and PHP Fatal error:
Call to a member functionget_data()
on boolean.
add_action( 'woocommerce_email_before_order_table', 'generate_code');
function generate_code(){
$order = wc_get_order( $order_id );
$order_data = $order->get_data(); // The Order data
$order_billing_email = $order_data['billing']['email'];
$order_total_tax = $order_data['total_tax'];
$connection = DBLogin();
$query = "INSERT INTO test_vouchers (voucher_id, email, redeemed, value) VALUES ('$string','$order_billing_email','0','$order_total_tax')";
}
What I am doing wrong? How can I solve this error?
And how can I store order total and customer email in a custom table in MySQL database?