0

I want to use a JS variable in JSON.

var add = mixItems[i][0] + "," + mixItems[i][1];
jQuery.getJSON("wp-content/plugins/proteinmixer/php/addtocart.php" , function(data){
});

PHP:

require_once('../../../../wp-blog-header.php'); 
header('Content-Type:application/json'); 
global $woocommerce; 
$productId = 66; 
$quantity = 10; 
//$add = $productId.",".$quantity.""; 
//$add = add; 
$addtocart = $woocommerce->cart->add_to_cart($add); 
echo json_encode($addtocart);

I need the variable ADD in JSON.

2
  • What do you mean js variable in Json? Function getJson will return Json object as data in your callback and what do you want to do after? add some fields to this object? Commented Sep 20, 2016 at 7:08
  • Your question is not explaining well. do you want json of items? if there are only items like item1,item2,item3. then your can just build an array like ["item1","item2","item3"]. or if you want json only. you can build key-value pair. [{ "item_name" : "item1" },{ "item_name" : "item2" },{ "item_name" : "item3" }]. Commented Sep 20, 2016 at 7:09

1 Answer 1

1

You can turn any Javascript value/object into a JSON string by using JSON.stringify():

var add = mixItems[i][0] + "," + mixItems[i][1];
var jsonAdd = JSON.stringify(add);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.