Ad
Add Some Data To Custom Database Table Once WooCommerce Order Is Placed
Please Guys,
i need to capture data from the checkout page form and save in a custom table.
1° i created a custom table: wp_personalinfo
2° below is the script that i'm using inside functions.php
/*
* Submit button name - 'woocommerce_checkout_place_order'
*/
if (!empty($_POST['woocommerce_checkout_place_order'])) {
/*
* Form Fields
*/
if (!empty($_POST['billing_first_name']) and
!empty($_POST['billing_last_name']) and
!empty($_POST['billing_phone']) and
!empty($_POST['billing_recommend']) and
!empty($_POST['billing_email'])) {
// DB Class Connection
global $wpdb;
// Sanitize the fields - form checkout.php
$billing_first_name = sanitize_text_field($_POST['billing_first_name']);
$billing_last_name = sanitize_text_field($_POST['billing_last_name']);
$billing_phone = sanitize_text_field($_POST['billing_phone']);
$billing_recommend = sanitize_text_field($_POST['billing_recommend']);
$billing_email = sanitize_text_field($_POST['billing_email']);
// Prefix db
$tablepsl = $wpdb->prefix.'personalinfo';
$datapsl = array(
'TB_NAME' => $billing_first_name,
'TB_LAST_NAME' => $billing_last_name,
'TB_PHONE' => $billing_phone,
'TB_RECOMENDATION' => $billing_recommend,
'TB_EMAIL' => $billing_email
);
$wpdb->insert($tablepsl, $datapsl);
} else {
echo 'All fields are mandatory';
}
}
No data it's been saved in the table wp_personalinfo.
What am i doing wrong please?
Ad
Answer
Well, Today is 21.10.2020 02:43 PM and i use the current woocommerce version is 4.6.0
This is what works for me.:
function porto_woocommerce_checkout_create_order( $order, $data ) {
$order = $order->save();
$billing_first_name = sanitize_text_field($data['billing_first_name']);
$billing_last_name = sanitize_text_field($data['billing_last_name']);
$billing_recommend = sanitize_text_field($data['billing_recommend']);
$billing_phone = sanitize_text_field($data['billing_phone']);
$billing_email = sanitize_text_field($data['billing_email']);
$payment_method = sanitize_text_field($data['payment_method']);
//$order_number = sanitize_text_field($data['order_number']);
// Prefix db
global $wpdb;
$tablepsl = $wpdb->prefix . 'personalinfo';
$data = array(
'tb_name' => $billing_first_name,
'tb_lastname' => $billing_last_name,
'tb_recomendation' => $billing_recommend,
'tb_phone' => $billing_phone,
'tb_email' => $billing_email,
'tb_numorder_id' => $order
); $wpdb->insert($tablepsl, $data);
}
add_action( 'woocommerce_checkout_create_order', 'porto_woocommerce_checkout_create_order', 10, 2 );
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad