EDIT: I'm adding code so that it can help:
in functions.php
// AJAX HOOKS TO LOAD PRODUCTS' INFO
add_action('wp_ajax_get_product_info', 'get_product_info' );
add_action('wp_ajax_nopriv_get_product_info', 'get_product_info' );
// SHOULD RUN SHORTCODE TO GET PRODUCT INFORMATION AND SEND IT BACK TO JS SCRIPT
function get_product_info() {
//Checks the nonce and kills the script if wrong
$nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'return_posts' ) )
die ( 'Wrong nonce!');
//Process Info
$pid = $_POST['product_id'];
header('Content-type: text/html');
// Returns the shortcode
echo do_shortcode("[ps_product_list id_product=".$pid." tpl=product-grid-exclusives.tpl]");
// We don't want anything else to run
exit;
}
// REGISTERS, LOADS & LOCALIZES product-store-ajax.js
if (!function_exists('load_scripts')) {
function load_scripts() {
if ( !is_admin() && is_category('teaser-online-exclusives') ) {
wp_register_script( 'product-ajax', get_stylesheet_directory_uri().'/js/product-store-ajax.js', array( 'jquery' ), '', true );
wp_enqueue_script('product-ajax');
wp_localize_script(
'product-ajax',
'productajaxvars',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'return_posts' ),
'callback' => 'get_product_info'
)
);
}
}
}
add_action('wp_enqueue_scripts', 'load_scripts');
And the JS file:
(function( $ ) {
$(function() {
$.ajax({
url: productajaxvars.ajax_url,
data: {
action: productajaxvars.callback, // callback defined in functions.php
nonce : productajaxvars.nonce,
product_id: 82},
success: function( response ) {
alert(response);
},
type: "POST",
});
});
})( jQuery );