• be_tnt

    (@be_tnt)


    Hello!

    I am trying to use ajax to add product in a session cart in a custom plugin (this is my first plugin dev).

    In the plugin file, I have started the session:

    function session_manager() {
    	    if (!session_id()) {
    	        session_start();
    	    }
    	}
    	add_action('init', 'session_manager',1);

    I have then added the js to the page using the wp_enqueue_script function and the wp_localize_script:

    function add_ld_advanced_cart_js() {
    		wp_enqueue_script(
    			'ld-advanced-cart',
    			LD_PLUGIN_JS_URL,
    			array('jquery')
    		);
    
    		// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
    		wp_localize_script( 'ld-advanced-cart', 'MyAjax', array(
    			'ajaxurl' => admin_url( 'admin-ajax.php' )
    			)
    		);
    
    	}
    	/* add javascript to page using this plugin */
    	add_action('wp_enqueue_scripts', 'add_ld_advanced_cart_js');

    I have defined the 2 ajax action:

    add_action( 'wp_ajax_nopriv_add-to-cart-submit', 'ajax_addtocart_submit' );
    add_action( 'wp_ajax_add-to-cart-submit', 'ajax_addtocart_submit' );

    But from the ajax_addtocart_submit, I do not have access to current session variable values, even if I add in this function:

    if( !session_id())
        session_start();

    If I use use my own ajax php file (not going through admin_ajax.php), this is working well.

    Did I miss something to make the SESSION available in the ajax part or it’s just not possible in the way WordPress is built??

    Thx in advance!

The topic ‘[ Plugin ajax and session ] Using admin_ajax remove session’ is closed to new replies.