Last active
August 29, 2015 14:23
-
-
Save vajrasar/5751fee166d0282665b0 to your computer and use it in GitHub Desktop.
AJAX with WP_Session [Read Details.html here for details]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | |
| Underlaying 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. | |
| Working php session - https://gist.github.com/vajrasar/8a8aa97de1b39f18ef29 | |
| Using WP_Sessions it sucessfully displays what it should the first time when the button is clicked but never on multiple clicks. Never. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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(); | |
| echo "<br />" . "Not empty cart -in init"; | |
| } | |
| } | |
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment