Ajax 似乎工作正常,但购物车内容不会按预期刷新。单击“添加到购物车”按钮后,我希望刷新购物车的内容。 就像现在一样,我必须手动刷新页面才能看到添加的产品。
我正在使用此功能将产品添加到我的 woocommerce 购物车:
function addToCart(p_id) {
jQuery.ajax({
type: 'POST',
url: '/wp/?post_type=product&add-to-cart='+p_id,
data: { 'product_id': p_id,
'quantity': amount},
success: function(response, textStatus, jqXHR){
console.log("Product added");
}/*,
dataType: 'JSON'*/
});
}
jQuery('#addToCart').click(function(e) {
e.preventDefault();
addToCart(prod_id["product_id"]);
return false;
});
是否可以在添加产品后仅刷新购物车?
最佳答案
这是可能的 - 您需要使用此代码的修改版本:
http://docs.woothemes.com/document/show-cart-contents-total/
编辑 - 以防将来出现 404
<a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
// Ensure cart contents update when products are added to the cart via AJAX (place the following in functions.php).
// Used in conjunction with https://gist.github.com/DanielSantoro/1d0dc206e242239624eb71b2636ab148
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');
function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
ob_start();
?>
<a class="cart-customlocation" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cart-customlocation'] = ob_get_clean();
return $fragments;
}
关于javascript - 添加产品后更新 Woocommerce 购物车 (jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13930421/