Skip to content

Instantly share code, notes, and snippets.

@arnaimbd
Created October 18, 2016 08:59
Show Gist options
  • Save arnaimbd/559904e080bdef103c6b75a0fdc57d7c to your computer and use it in GitHub Desktop.
Save arnaimbd/559904e080bdef103c6b75a0fdc57d7c to your computer and use it in GitHub Desktop.
Debuging ajax call on WordPress
<?php
add_action( "wp_ajax_stock_form", function() {
if ( !isset( $_POST['data'] ) ) {
wp_send_json_error( ['msg' => 'Wrong data'] );
}
$args = [];
wp_parse_str( $_POST['data'], $args );
$id = $_POST['warehouse_id'];
file_put_contents( __DIR__ . "/debug.txt", print_r( $args, 1 ) );
wp_send_json_success( $data );
} );
;(function($) {
$('#stockForm').on("submit", function(e) {
e.preventDefault();
// console.log( $(this).data("warehouse-id") );
$.post(
wc_add_to_cart_params.ajax_url,
{
action: "stock_form",
data: $(this).serialize(),
warehouse_id: $(this).data("warehouse-id"),
}
).done(function(res) {
console.log(res);
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment