1

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

  • Is that a typo using toArray in vg_show_post_id(), shouldn't it be $cart = $wp_session['wpscart']->toArray();? – bonger Jun 29 '15 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 Jun 29 '15 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 Jun 29 '15 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 Jun 29 '15 at 13:46
  • Edited the question, if that helps (see the last para) @bonger – vajrasar Jul 2 '15 at 7:02
0

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 },
),
-1

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.....
  • I don't think we need to do session_start when we are using WP Session Manager plugin as it handles that. What say? – vajrasar Jul 2 '15 at 6:50
  • 1
    if( !session_id() ) This is important, In WP we can't use session with out session ID, let it try, it helps me in my plugin. – Hari Om Gupta Jul 2 '15 at 7:00
  • Done. Still it shows the Fatal Error that I just added to the main question. (See the last para). – vajrasar Jul 2 '15 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.