由于默认行为打破了我认为的购物体验,我想使用Ajax提交woocommerce评论表。
我已经使用jQuery.ajax()发布表单但是如果有人没有登录,我所得到的只是错误。即使填写了所有表单字段(评级,评论,作者和电子邮件地址)。
请帮帮我!
编辑 - 添加了我尝试过的代码
jQuery('document').ready(function($){
$( '#review_form' ).append( '<div id="comment-status"></div>' );
var commentform = $('#commentform');
var statusDiv = $('#comment-status')
commentform.submit(function(){
var formdata = commentform.serialize();
var formurl = commentform.attr('action');
statusDiv.html( '<p class="wdpajax-error">Bezig met verwerken...</p>' );
$.ajax({
type: 'post',
url: formurl,
data: formdata,
error: function( XMLHttpRequest, textStatus, errorThrown ) {
console.log(XMLHttpRequest);
console.log(textStatus);
console.log(errorThrown);
statusDiv.html( '<p class="alert alert-danger" >Je hebt een van de verplichte velden leeg gelaten of te snel nog een reactie geplaatst</p>' );
},
success: function( data, textStatus ) {
if( textStatus == "success" ) {
commentform.hide();
statusDiv.html(' <p class="alert alert-success" ><strong>Bedankt voor je beoordeling!</strong> Het kan even duren voordat hij op de site verschijnt, aangezien we hem nog moeten controleren</p>' );
} else {
statusDiv.html( '<p class="alert alert-warning">Je hebt een van de verplichte velden leeg gelaten of te snel nog een reactie geplaatst</p>' );
commentform.find( 'textarea[name=comment]' ).val( '' );
}
}
});
return false;
});
});