I am trying to pass some JSON via AJAX to a php script, here is my javascript:
jQuery(document).ready(function($) {
/**
* AJAX add to cart
*/
$( ".single_add_to_cart_button" ).each(function() {
var el = $(this);
el.click(function(e) {
var product_data = $("#jsonVariations").val();
e.preventDefault();
$(this).text('Adding to cart');
// try ajax
$.ajax({
url: myAjax.ajaxurl,
type: "POST",
data: {
action: 'add_bv',
product_data: product_data,
},
dataType: "json",
//contentType: "application/json",
success: function (result) {
el.text("Added to cart");
},
error: function (xhr, ajaxOptions, thrownError) {
el.text("Not added to cart");
//alert(xhr.status);
alert(thrownError);
}
});
return false;
});
});
});
The JSON is valid (tested via jsonlint) and is somehting like this:
`[{"variationQty":5,"variationID":"50","variationSize":"2xl","variationColour":"grey"},{"variationQty":10,"variationID":"51","variationSize":"2xl","variationColour":"navy"}]
My php script is:
$product_data = $_POST['product_data'];
$product_data = json_decode($product_data, true);
foreach ($product_data as $product) {
$product_qty = intval( $product->variationQty );
$product_id = 24;
$product_variation_id = $product->variationID;
$product_variation = array(
'colour' => $product->variationColour,
'size' => $product->variationSize,
);
WC()->cart->add_to_cart( $product_id, $product_qty, $product_variation_id, $product_variation );
}
I have trialed setting the $product_data
variable manually to the json and it works perfectly, for some reason it wont pass the JSON
properly. I have also tried using JSON
stringify and set the content type, when this happens I get the AJAX
success function but the php script doesn't seem to execute.
I guess this is the : json_decode($product_data, true)
the return array ,not an object. It can be json_decode($product_data)
or json_decode($product_data, false)
. Hope I can help you.
Element IDs should be unique within the entire document.
is there is only one div with jsonVariations ID
My Mistake I have not fully understand the Problem.
In POSTING
json encoded
data using ajax, when it reaches to the server it is already converted to a $_POST[...]
array
your $_POST
would be:
$_POST:
array(
'action' => 'add_bv',
'product_data' => array(...the content of product_data...)
);
In your case, I think you dont have to decode the posted json data because you just have to use it as is.
$product_data = $_POST['product_data']; //Correct, allready an array
$product_data = json_decode($product_data, true); //Not needed
Thanks to @vsogrimen I determined the problem was on the php side. For some reason the JSON object had quotes escaped. I updated my code to strip these slashes and it works perfect. Here is my final code:
function prefix_ajax_add_bv() {
$product_data = stripslashes($_POST['product_data']);
$product_data = json_decode($product_data, false);
foreach ($product_data as $product) {
$product_qty = intval( $product->variationQty );
$product_id = 24;
$product_variation_id = $product->variationID;
$product_variation = array(
'colour' => $product->variationColour,
'size' => $product->variationSize,
);
WC()->cart->add_to_cart( $product_id, $product_qty, $product_variation_id, $product_variation, array('_my_data','000000000000000000000000000000') );
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.