【问题标题】:Add some data to custom database table once WooCommerce order is placed下 WooCommerce 订单后,将一些数据添加到自定义数据库表
【发布时间】:2021-01-28 17:14:34
【问题描述】:

请大家,
我需要从结帐页面表单中获取数据并保存在自定义表格中。

1° 我创建了一个自定义表格:wp_personalinfo

下面2°是我在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';
    }

}

wp_personalinfo 表中没有保存任何数据。

请问我做错了什么?

【问题讨论】:

  • 你好@LoicTheAztec,再一次,非常感谢你的大力帮助,但不幸的是,在这种情况下,这个脚本对我不起作用,我不知道为什么。但我会发布对我有用的 Hook。今天是 21.10.2020 02:43 PM,我使用当前的 woocommerce 版本是 4.6.0

标签: php database wordpress woocommerce hook


【解决方案1】:

你试过var_dump($_POST)

可以钩woocommerce_checkout_place_order不工作

让我们试一试woocommerce_checkout_process

【讨论】:

  • 你好@hùng-nguyễn 谢谢你的光!是的,我尝试使用 var_dump($_POST),但我没有肯定的答案!有效的 Hook 是另一个
【解决方案2】:

好吧,今天是 21.10.2020 02:43 PM,我使用当前的 woocommerce 版本是 4.6.0
这对我有用。:

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 );

【讨论】:

    猜你喜欢
    • 2016-10-12
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 2022-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多