0

我需要为现有的 Wordpress 网站添加订单。从主页单击项目后,用户将被重定向到该项目的订单。现在这是一个挑战,如果用户之前为该项目下过订单,则必须自动填充/自动填充订单表中的条目。

我使用 Ninja Forms 制作了表单,并为自动填充功能制作了一个自定义插件。这是我编写的自定义插件的一些代码:

<?php
/*
...
*/

global $my_custom_plugin_table_version;
$my_custom_plugin_table_version = '1.0';  // table version if need to update

register_activation_hook( __FILE__, 'custom_plugin_create_db' );
function custom_plugin_create_db() {
    global $wpdb;
    global $my_custom_plugin_table_version;

    $table_name = $wpdb->prefix . "my_custom_plugin_table";
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        order_id VARCHAR(140) NOT NULL,
        user_company VARCHAR(250) NOT NULL,
        user_name VARCHAR(250) NOT NULL,
        user_address VARCHAR(255) NOT NULL,
        user_city VARCHAR(50) NOT NULL,
        user_state VARCHAR(50) NOT NULL,
        user_zip VARCHAR(50) NOT NULL,
        user_phone VARCHAR(50) NOT NULL,
        user_email VARCHAR(50) NOT NULL,
        order_quantity INT(11) NOT NULL,
        PRIMARY KEY  (order_id) 
    ) $charset_collate;";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );

    add_option( 'my_custom_plugin_table_version', $my_custom_plugin_table_version );
}

function custom_plugin_autofill_form( $default_value, $field_type, $field_settings ) {
    $form_id = 3;
    $models = Ninja_Forms()->form( $form_id )->get_subs();
    $id = current( $models )->get_id();
    $sub = Ninja_Forms()->form()->get_sub( $id );

    foreach ( $sub->get_field_values() as $key => $value ) {
        if ( $field_settings['key'] == $key ) {
            $default_value = $value;
        }
    }

    return $default_value;
}

add_action( 'init', 'check_for_previous_order' );
function check_for_previous_order() {
    global $wpdb;

    $table_name = $wpdb->prefix . "my_custom_plugin_table";
    $user_id = get_current_user_id();
    $project_name = 'project-1';
    $order_id = $project_name . '-' . $user_id;

    $result = $wpdb->get_row( "SELECT * FROM $table_name WHERE order_id = '$order_id'" );

    if ( $result != NULL )
        add_filter( 'ninja_forms_render_default_value', 'custom_plugin_autofill_form', 10, 3 );
}

do_action( 'check_for_previous_order' );

add_action( 'ninja_forms_after_submission', 'custom_plugin_save_db' );
function custom_plugin_save_db( $form_data ) {
    global $wpdb;
    $table_name = $wpdb->prefix . "my_custom_plugin_table";
    $submitted_data = [];

    foreach ( $form_data[ 'fields' ] as $field ) {
        $key = $field[ 'key' ];
        $value = $field[ 'value' ];
        $submitted_data[ $key ] = $value;
    }

    $wpdb->replace( $table_name, array(
        'order_id' => $submitted_data[ 'order_project_id' ] . '-' . get_current_user_id(),
        'user_company' => $submitted_data[ 'user_company' ],
        'user_name' => $submitted_data[ 'user_name' ],
        'user_address' => $submitted_data[ 'user_address' ],
        'user_city' => $submitted_data[ 'user_city' ],
        'user_state' => $submitted_data[ 'user_state' ],
        'user_zip' => $submitted_data[ 'user_zip' ],
        'user_phone' => $submitted_data[ 'user_phone' ],
        'user_email' => $submitted_data[ 'user_email' ],
        'order_quantity' => $submitted_data[ 'order_quantity' ]
    ) );
}

它侦听 NF 表单并在提交后将值保存到表中。当显示 NF 表单时,它会检查数据库中的任何先前条目。我将该函数包装check_for_previous_order在一个动作中以使该get_current_user_id函数正常工作。

如您所见,我通过应用order_id只是项目名称和当前用户 ID 的组合来保存以前的每个订单。问题在于检索它们。我只在代码上写了$project_name$form_id,但我需要它们是动态的。我为项目制作了一个自定义帖子类型项目,每个项目都是一个项目帖子。项目名称是 post slug。但我不知道如何得到蛞蝓。

我已经尝试过全局 $post 方法。它返回 NULL。global $pagenow返回index.php

4

1 回答 1

1

我不认为在调用钩子$post之前设置了对象。add_action('init');

您可以尝试将其更改为add_action('template_redirect', 'check_for_previous_order');

<?php
add_action( 'template_redirect', 'check_for_previous_order' );
function check_for_previous_order() {
    global $wpdb;
    global $post;
    echo $post->ID;
    //...
}
?>

要仅针对具有易于访问表单 ID 的表单的页面触发此操作,您可以尝试将其替换add_action('template_redirect','check_for_previous_order')为:

<?php
function check_for_previous_order($form_id) {

    //form id comes in as an argument now
    global $post;

}
add_action('ninja_forms_display_init', 'check_for_previous_order', 10, 1);
?>
于 2017-07-10T04:56:45.427 回答