Answer the question
In order to leave comments, you need to log in
How to pass function parameters via ajax?
Here is the php function code to add item to cart
add_action('wp_ajax_myajax', 'myajax');
add_action('wp_ajax_nopriv_myajax', 'myajax');
function myajax($product_id, $qty) {
global $woocommerce;
$woocommerce->cart->add_to_cart($product_id, $qty);
die();
}
jQuery(document).ready(function($){
$.ajax({
url: "wp-admin/admin-ajax.php",
data: 'myajax'
});
});
Answer the question
In order to leave comments, you need to log in
the code is initially incorrect, there is no action parameter
jQuery(document).ready(function($){
$.ajax({
url: "wp-admin/admin-ajax.php",
data: {
action: 'myajax',
product_id: $product_id,
qty : $qty,
};
});
});
function myajax() {
$product_id= intval( $_POST['product_id'] );
$qty= intval( $_POST['qty'] );
global $woocommerce;
$woocommerce->cart->add_to_cart($product_id, $qty);
die();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question