Dawid Lisiecki Dawid Lisiecki - 5 months ago 47
jQuery Question

Remove product in the custom cart using ajax in woocommerce

I have a problem removing products from the cart. My code removes the product in ajax but does not refresh the cart. This is my code:

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;
$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">
var product_id = jQuery(this).attr("data-product_id");
type: 'POST',
dataType: 'json',
url: "myURL",
data: { action: "product_remove",
product_id: product_id
},success: function(data){
return false;
<?php $fragments['.cart-customlocation'] = ob_get_clean();
return $fragments;

AND This is a function called in ajax.

function product_remove() {

global $woocommerce;

$id = $_POST['product_id'];


I will be grateful for any help.

Answer Source

You're removing the item from your php object, but if you want to remove it from the current view you have to select the item and remove it with jquery inside of the success function of your're ajax call. Something like...

,success: function(data){
    $([selector for your product line]).remove();

I can't see the html of your product lines (to see the format of what you want to remove). If you paste it I'll give you a hand.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download