Php Ajax添加到WooCommerce产品页面上的购物车
我正试图让AJAX为我的网站的WooCommerce产品页面上的“添加到购物车”按钮工作。我使用下面的代码片段来获得AJAX功能,除了代码的最后一部分之外,所有这些都正常工作。 单击“添加到购物车”按钮时,我没有看到任何成功/通知/错误消息。项目被添加到购物车,但没有视觉通知(除了按钮上的加载动画)让用户知道项目已成功添加 如果我删除下面代码的最后一部分“添加通知片段”,我会在添加项目时看到通知,但只有在重新加载页面后才会看到 我正试图使它这样,当添加到购物车按钮被点击,项目被添加到购物车,并显示成功消息,或相应的错误消息。我做错了什么Php Ajax添加到WooCommerce产品页面上的购物车,php,jquery,ajax,wordpress,woocommerce,Php,Jquery,Ajax,Wordpress,Woocommerce,我正试图让AJAX为我的网站的WooCommerce产品页面上的“添加到购物车”按钮工作。我使用下面的代码片段来获得AJAX功能,除了代码的最后一部分之外,所有这些都正常工作。 单击“添加到购物车”按钮时,我没有看到任何成功/通知/错误消息。项目被添加到购物车,但没有视觉通知(除了按钮上的加载动画)让用户知道项目已成功添加 如果我删除下面代码的最后一部分“添加通知片段”,我会在添加项目时看到通知,但只有在重新加载页面后才会看到 我正试图使它这样,当添加到购物车按钮被点击,项目被添加到购物车,并显
/**
* JS for AJAX Add to Cart handling
*/
function product_page_ajax_add_to_cart_js() {
?><script type="text/javascript" charset="UTF-8">
jQuery(function($) {
$('form.cart').on('submit', function(e) {
e.preventDefault();
var form = $(this);
form.block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } });
var formData = new FormData(form.context);
formData.append('add-to-cart', form.find('[name=add-to-cart]').val() );
// Ajax action.
$.ajax({
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ),
data: formData,
type: 'POST',
processData: false,
contentType: false,
complete: function( response ) {
response = response.responseJSON;
if ( ! response ) {
return;
}
if ( response.error && response.product_url ) {
window.location = response.product_url;
return;
}
// Redirect to cart option
if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
window.location = wc_add_to_cart_params.cart_url;
return;
}
var $thisbutton = form.find('.single_add_to_cart_button'); //
// var $thisbutton = null; // uncomment this if you don't want the 'View cart' button
// Trigger event so themes can refresh other areas.
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
// Remove existing notices
$( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
// Add new notices
form.closest('.product').before(response.fragments.notices_html)
form.unblock();
}
});
});
});
</script><?php
}
add_action( 'wp_footer', 'product_page_ajax_add_to_cart_js' );
/**
* Add to cart handler.
*/
function ajax_add_to_cart_handler() {
WC_Form_Handler::add_to_cart_action();
WC_AJAX::get_refreshed_fragments();
}
add_action( 'wc_ajax_add_to_cart', 'ajax_add_to_cart_handler' );
add_action( 'wc_ajax_nopriv_add_to_cart', 'ajax_add_to_cart_handler' );
// Remove WC Core add to cart handler to prevent double-add
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
/**
* Add fragments for notices.
*/
function ajax_add_to_cart_add_fragments( $fragments ) {
$all_notices = WC()->session->get( 'wc_notices', array() );
$notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );
ob_start();
foreach ( $notice_types as $notice_type ) {
if ( wc_notice_count( $notice_type ) > 0 ) {
wc_get_template( "notices/{$notice_type}.php", array(
'messages' => array_filter( $all_notices[ $notice_type ] ),
) );
}
}
$fragments['notices_html'] = ob_get_clean();
wc_clear_notices();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'ajax_add_to_cart_add_fragments' );
/**
*JS for AJAX添加到购物车处理
*/
功能产品页面ajax添加到购物车js(){
?>
jQuery(函数($){
$('form.cart')。关于('submit',函数(e){
e、 预防默认值();
变量形式=$(此);
block({message:null,overlycss:{background:'#fff',不透明性:0.6}});
var formData=新的formData(form.context);
formData.append('add-to-cart',form.find('[name=add-to-cart]')).val();
//Ajax操作。
$.ajax({
url:wc_add_to_cart_params.wc_ajax_url.toString().replace(“%%endpoint%%”,“add_to_cart”),
数据:formData,
键入:“POST”,
processData:false,
contentType:false,
完成:功能(响应){
response=response.responseJSON;
如果(!响应){
返回;
}
if(response.error&&response.product\u url){
window.location=response.product\u url;
返回;
}
//重定向到购物车选项
如果(wc_add_to_cart_params.cart_redirect_after_add=='yes'){
window.location=wc_add_to_cart_params.cart_url;
返回;
}
var$thisbutton=form.find('.single_add_to_cart_button')//
//var$thisbutton=null;//如果不需要“查看购物车”按钮,请取消对此的注释
//触发事件,以便主题可以刷新其他区域。
$(document.body).trigger('added_to_cart',[response.fragments,response.cart_hash,$thisbutton]);
//删除现有通知
$('.woocommerce错误,.woocommerce消息,.woocommerce信息').remove();
//添加新通知
form.closest('.product').before(response.fragments.notices_html)
form.unblock();
}
});
});
});
替换以下代码
/**
* Add fragments for notices.
*/
function ajax_add_to_cart_add_fragments( $fragments ) {
$all_notices = WC()->session->get( 'wc_notices', array() );
$notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );
ob_start();
foreach ( $notice_types as $notice_type ) {
if ( wc_notice_count( $notice_type ) > 0 ) {
wc_get_template( "notices/{$notice_type}.php", array(
'notices' => array_filter( $all_notices[ $notice_type ] ),
) );
}
}
$fragments['notices_html'] = ob_get_clean();
// wc_clear_notices();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'ajax_add_to_cart_add_fragments' );