I am not getting success response after inserting/updating record to the database. Instead ajax returning error: function(response)
with Something went wrong!
message.
$this->gs_db
is the database class instance and$this->gs_db->db
is$wpdb
/**
* Add product group into the cart
*
* @param int $group_id group id
* @param int $product_group product group id
* @param int|bool $group_user group user / logged in id
*
* @return int inserted row id if new else update row id
*/
public function add_pg_to_cart($group_id, $product_group, $group_user = FALSE)
{
$this->group_id = $group_id;
$this->qty = 1;
$this->product_group = $product_group;
$this->group_admin = gs_get_group_admin($this->group_id);
$this->group_user = ($group_user) ? absint($group_user) : absint(get_current_user_id());
// setup data to insert
$this->data = [
'product_group' => $this->product_group,
'qty' => $this->qty,
'group_id' => $this->group_id,
'group_admin' => $this->group_admin,
'group_user' => $this->group_user,
];
// setup data format
$this->format = [
'%d',
'%d',
'%d',
'%d',
'%d',
];
// insert / update query
$table = $this->gs_db->get_pg_cart_table();
$query = "INSERT INTO {$table} (product_group, qty, group_id, group_admin, group_user, added_at)
VALUES('%d', '%d', '%d', '%d', '%d', now()) ON DUPLICATE KEY UPDATE qty = qty + 1, added_at = now()";
$prepare = $this->gs_db->db->prepare($query, $this->product_group, $this->qty, $this->group_id, $this->group_admin, $this->group_user);
$result = $this->gs_db->db->get_row($prepare);
// get the id for inserted record
$this->id = $this->gs_db->db->insert_id;
return $this->id;
}
(function ($) {
$(function () {
let response_alert = $('#response');
response_alert.hide();
$('form.gsGpAddToCart').on('submit', function (e) {
e.preventDefault();
let formData = $(this).serialize();
$.ajax({
method: 'POST',
dataType: 'json',
url: ajax_vars.ajax_url,
data: formData + "&action=gs_pg_add_to_cart&nonce=" + ajax_vars.nonce,
success: function (response) {
console.log(response);
let response_alert = $('#response');
if (response.success === true) {
response_alert.fadeIn().prepend(alerts('success', response.data));
}
if (response.success === false) {
response_alert.fadeIn().prepend(alerts('error', response.data));
}
},
error: function (response) {
let error_message = (response.data === undefined) ? 'Something went wrong!' : response.data;
response_alert.fadeIn().prepend(alerts('error', error_message));
gs_alert_stats();
},
complete: function () {
}
});
return false;
});
// control dynamically created error with JS - close/hide
function gs_alert_stats() {
let gs_alert = $('.gs-message');
$('.gs-close').on('click', function () {
alerts('Clicked');
$(this).parent(gs_alert).fadeOut();
});
}
});
})(jQuery);
Javascript is returning this error response Something went wrong!
which is in error: function (response)