如何获得自定义帖子类型的帖子?

时间:2017-07-10 03:09:05

标签: php wordpress e-commerce backend ninja-forms

我需要为现有的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保存了以前的每个订单,$project_name只是项目名称和当前用户ID的组合。问题在于检索它们。我只在代码上写了$form_idglobal $pagenow,但我需要它们是动态的。我为项目制作了一个自定义的帖子类型项目,每个项目都是一个项目帖子。项目名称是post slug。但我无法弄清楚如何获得slu ..

我尝试了全局$ post方法。它返回NULL。 index.php会返回```{r corr-matrix} library(xtable) kable(upper, booktabs = T, caption = "The correlation matrix") %>% kable_styling(font_size = 7) %>% landscape() ```

1 个答案:

答案 0 :(得分: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);
?>