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();
2 Answers
Reset to default 1I 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.....
toArray
invg_show_post_id()
, shouldn't it be$cart = $wp_session['wpscart']->toArray();
? – bonger Commented Jun 29, 2015 at 12:59vg_session_start()
seems odd, if you doif( 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