我在從購物車中刪除產品時遇到問題。我的代碼在ajax中刪除產品,但不刷新購物車。這是我的代碼:使用woocommerce中的ajax刪除自定義購物車中的產品
add_filter('woocommerce_add_to_cart_fragments','woocommerce_header_add_to_cart_fragment'); function woocommerce_header_add_to_cart_fragment($fragments) {
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
ob_start();
$cart_count = sprintf(_n('%d', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);
?>
<div data-toggle="dropdown" class="cart-customlocation" [...] ?></span>
<div class="dropdown-menu custom-cart">
<?php foreach($cart as $cart_item_key => $item){ ?>
<?php $img_url = $item['data']->image_id; ?>
<img src="<?php echo wp_get_attachment_url($img_url); ?>" alt="">
[...] <- content
<a class="remove-product" data-product_id="<?php echo $cart_item_key ?>">X</a>
<?php } ?>
<script type="text/javascript">
jQuery('.remove-product').click(function(){
var product_id = jQuery(this).attr("data-product_id");
console.log(product_id);
jQuery.ajax({
type: 'POST',
dataType: 'json',
url: "myURL",
data: { action: "product_remove",
product_id: product_id
},success: function(data){
}
});
return false;
});
</script>
</div>
</div>
<?php $fragments['.cart-customlocation'] = ob_get_clean();
return $fragments;
,這是一個名爲AJAX功能:
function product_remove() {
global $woocommerce;
$id = $_POST['product_id'];
$woocommerce->cart->remove_cart_item($id);}
謝謝你回答我的問題。我理解這個解決方案,我會測試它。請給我第二個。 :) –