Get a JSON Feed From the Product Page
If you would like to be able to get a JSON (or JSONP for that matter) feed of a single product from the product page, you can use this code snippet. Since you’ll lose your modifications when the FoxyShop plug-in updates if you modify the original plug-in file, you’ll want to make a copy of the file themefiles/foxyshop-single-product.php, add the code snippet below to the copied file, then upload the copy to the WordPress folder of your selected theme (typically at /wordpress/wp-content/themes/your-selected-theme).
You’ll insert this at the very top of the file (before get_header());
if (isset($_GET['json'])) {
while (have_posts()) : the_post();
global $product;
$product = foxyshop_setup_product();
$jsonval = json_encode($product);
if (isset($_REQUEST['callback'])) { //This creates JSONP wrapper
$jsonval = sanitize_title($_REQUEST['callback']) . "($jsonval)";
}
echo $jsonval;
die;
endwhile;
}
To access the JSON feed, use the product’s URL plus
?json=1
For example:
https://wordpress.your-site.com/products/your-product/?json=1
Posted in: Helpful Code Snippets

