最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

session - WP_Session not acting with AJAX

programmeradmin4浏览0评论

The concept is to put a Button in Post Content which the user will click.

Two things - The ID of the post and Number of times the user clicks the Button should be displayed.

Reason - With this we are presenting Posts as Products and with that button user can add products, and number of clicks on the button will signify the quantity that he wants.

The logic is working well with the normal php session, but not working with WP_Session using WP Session Manager plugin by Eric Mann which is considered as a standard way for using sessions in WP.

Working php session code - Github

Using WP_Sessions it successfully displays what it should the first time when the button is clicked but never on multiple clicks. Never.

This is in Custom.js file --->

jQuery( '.click' ).on( 'click', function() {

        var post_id = jQuery( this ).attr( "data-id" );
        var thisis = jQuery( this );

        jQuery.ajax({

            url: postcustom.ajax_url,
            type: 'post',
            data: {
                action: 'vg_show_post_id',
                post_id: post_id,
            },
            success: function( response ) {
                jQuery( thisis.next( '#after-ajax' )  ).fadeIn( "slow" );
                jQuery( thisis.next( '#after-ajax' )  ).text( "Item is added to the cart!" );
                jQuery( '#session-sidebar' ).html( response );
                jQuery( thisis.next( '#after-ajax' ) ).fadeOut( "slow" );
            }

        });

    });

This is in functions.php File --->

<?php

/*****************************
*
* Ajax Stuff
*
********************************/

function vg_session_start() {
    global $wp_session;
    global $cart;
    global $counter;

    $wp_session = WP_Session::get_instance();

    if( !empty( $cart ) ) {
        $cart = $wp_session['wpscart']->toArray();
    }
}
add_action( 'init', 'vg_session_start' ); // Starting Session

function ajax_test_enqueue_scripts() {
    wp_enqueue_script( 'customjs', get_stylesheet_directory_uri() . '/js/custom.js', array('jquery'), '1.0', true );
    wp_localize_script( 'customjs', 'postcustom', array(
                                'ajax_url' => admin_url( 'admin-ajax.php' )
                            ));
}
add_action( 'wp_enqueue_scripts', 'ajax_test_enqueue_scripts' ); // Enqueueing Scripts

function vg_after_entry() {
    ?>
    <button type="button" class="click" href="#" data-id="<?php echo get_the_ID(); ?>">Submit</button>
    <div id="after-ajax">
        <!-- to do post ajax stuff -->
    </div>
    <?php
}
add_action( genesis_entry_footer, vg_after_entry ); // Adding button in post content

function vg_show_post_id() {
    global $wp_session;
    global $cart;

    $wp_session = WP_Session::get_instance();

    $cart = $wp_session['wpscart']->toArray();

    $p_id = $_REQUEST['post_id'];
    $title = get_the_title( $p_id );

    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {

            if ( !empty( $cart ) && array_key_exists( $p_id, $cart ) ) {

                $cnt = $cart[$p_id];
                $cnt++;
                $cart[$p_id] = $cnt;

            } else {

                $cart[$p_id] = 1;

            }

            foreach( $cart as $key=>$value ) {
                echo "<br />" . get_the_title( $key ) . " " . $value . " units";
                echo "<hr />";
            }

            $wp_session['wpscart'] = $cart;
            die();

    } else {
        echo "Not in admin-ajax";
    }

}
add_action( 'wp_ajax_nopriv_vg_show_post_id', 'vg_show_post_id' ); // for ajax
add_action( 'wp_ajax_vg_show_post_id', 'vg_show_post_id' ); // for ajax

Just to add to this, one major error shows up everytime - Fatal error: Call to a member function toArray() on a non-object in /home/xxxxx/public_html/bcm/wp-content/themes/genesis-sample/functions.php on line 88 which has this code - $cart = $wp_session['wpscart']->toArray();

The concept is to put a Button in Post Content which the user will click.

Two things - The ID of the post and Number of times the user clicks the Button should be displayed.

Reason - With this we are presenting Posts as Products and with that button user can add products, and number of clicks on the button will signify the quantity that he wants.

The logic is working well with the normal php session, but not working with WP_Session using WP Session Manager plugin by Eric Mann which is considered as a standard way for using sessions in WP.

Working php session code - Github

Using WP_Sessions it successfully displays what it should the first time when the button is clicked but never on multiple clicks. Never.

This is in Custom.js file --->

jQuery( '.click' ).on( 'click', function() {

        var post_id = jQuery( this ).attr( "data-id" );
        var thisis = jQuery( this );

        jQuery.ajax({

            url: postcustom.ajax_url,
            type: 'post',
            data: {
                action: 'vg_show_post_id',
                post_id: post_id,
            },
            success: function( response ) {
                jQuery( thisis.next( '#after-ajax' )  ).fadeIn( "slow" );
                jQuery( thisis.next( '#after-ajax' )  ).text( "Item is added to the cart!" );
                jQuery( '#session-sidebar' ).html( response );
                jQuery( thisis.next( '#after-ajax' ) ).fadeOut( "slow" );
            }

        });

    });

This is in functions.php File --->

<?php

/*****************************
*
* Ajax Stuff
*
********************************/

function vg_session_start() {
    global $wp_session;
    global $cart;
    global $counter;

    $wp_session = WP_Session::get_instance();

    if( !empty( $cart ) ) {
        $cart = $wp_session['wpscart']->toArray();
    }
}
add_action( 'init', 'vg_session_start' ); // Starting Session

function ajax_test_enqueue_scripts() {
    wp_enqueue_script( 'customjs', get_stylesheet_directory_uri() . '/js/custom.js', array('jquery'), '1.0', true );
    wp_localize_script( 'customjs', 'postcustom', array(
                                'ajax_url' => admin_url( 'admin-ajax.php' )
                            ));
}
add_action( 'wp_enqueue_scripts', 'ajax_test_enqueue_scripts' ); // Enqueueing Scripts

function vg_after_entry() {
    ?>
    <button type="button" class="click" href="#" data-id="<?php echo get_the_ID(); ?>">Submit</button>
    <div id="after-ajax">
        <!-- to do post ajax stuff -->
    </div>
    <?php
}
add_action( genesis_entry_footer, vg_after_entry ); // Adding button in post content

function vg_show_post_id() {
    global $wp_session;
    global $cart;

    $wp_session = WP_Session::get_instance();

    $cart = $wp_session['wpscart']->toArray();

    $p_id = $_REQUEST['post_id'];
    $title = get_the_title( $p_id );

    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {

            if ( !empty( $cart ) && array_key_exists( $p_id, $cart ) ) {

                $cnt = $cart[$p_id];
                $cnt++;
                $cart[$p_id] = $cnt;

            } else {

                $cart[$p_id] = 1;

            }

            foreach( $cart as $key=>$value ) {
                echo "<br />" . get_the_title( $key ) . " " . $value . " units";
                echo "<hr />";
            }

            $wp_session['wpscart'] = $cart;
            die();

    } else {
        echo "Not in admin-ajax";
    }

}
add_action( 'wp_ajax_nopriv_vg_show_post_id', 'vg_show_post_id' ); // for ajax
add_action( 'wp_ajax_vg_show_post_id', 'vg_show_post_id' ); // for ajax

Just to add to this, one major error shows up everytime - Fatal error: Call to a member function toArray() on a non-object in /home/xxxxx/public_html/bcm/wp-content/themes/genesis-sample/functions.php on line 88 which has this code - $cart = $wp_session['wpscart']->toArray();

Share Improve this question edited Jul 2, 2015 at 7:01 vajrasar asked Jun 27, 2015 at 10:53 vajrasarvajrasar 1693 silver badges19 bronze badges 9
  • Is that a typo using toArray in vg_show_post_id(), shouldn't it be $cart = $wp_session['wpscart']->toArray();? – bonger Commented Jun 29, 2015 at 12:59
  • @bonger Yeah that was a typo, fixed it. Actually I also some test code (counters and if checks etc) within this code which I used to debug the issue but deleted that while asking here. So. Thanks for the catch. – vajrasar Commented Jun 29, 2015 at 13:02
  • Just to add, the problem is still the same. That typo just occurred when I was posting to StackOverflow. So, am still desperately looking for answers. – vajrasar Commented Jun 29, 2015 at 13:10
  • The initialization in vg_session_start() seems odd, if you do if( empty( $wp_session['wpscart'] ) ) $wp_session['wpscart'] = array(); before the ! empty( $cart ) test (which itself seems pointless) then does it work? (Also check for errors in your "php_errors.log"). – bonger Commented Jun 29, 2015 at 13:46
  • Edited the question, if that helps (see the last para) @bonger – vajrasar Commented Jul 2, 2015 at 7:02
 |  Show 4 more comments

2 Answers 2

Reset to default 1

I think in your code you must add this line for ajax acting

jQuery.ajax({
type    : 'POST',
url     : '<?php echo admin_url('admin-ajax.php'); ?>',
data    : { action : 'vg_show_post_id', post_id: $post_id, data-id: true },
),

I think you need to start session at init action, find the piece of code given below.. may be helpful to you. :)

    add_action('init','vg_session_start');
        function vg_session_start(){
         if( !session_id() )
           session_start();
         }
         ...... your code.....
发布评论

评论列表(0)

  1. 暂无评论