16

I would like to remove the product in the woocommerce cart using ajax without click the link.

If you have encounter this kind of functionality, please help us.

add_action( 'wp_footer', 'add_js_to_wp_wcommerce');

function add_js_to_wp_wcommerce(){ ?>
    <script type="text/javascript">
    jQuery('.remove-product').click(function(){
        var product_id = jQuery(this).attr("data-product_id");
        jQuery.ajax({
            type: 'POST',
            dataType: 'json',
            url: "/wp-admin/admin-ajax.php",
            data: { action: "product_remove", 
                    product_id: product_id
            },success: function(data){
                console.log(data);
            }
        });
        return false;
    });
    </script>
<?php }

add_action( 'wp_ajax_product_remove', 'product_remove' );
add_action( 'wp_ajax_nopriv_product_remove', 'product_remove' );
function product_remove() {
    global $wpdb, $woocommerce;
    session_start();
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item){
        if($cart_item['product_id'] == $_POST['product_id'] ){
            // Remove product in the cart using  cart_item_key.
            $woocommerce->cart->get_remove_url($cart_item_key);
        }
    }
    print_r($woocommerce->cart->get_cart());
    //echo json_encode(array('status' => 0));
    exit();
}
user3331550
  • 161
  • 1
  • 1
  • 4

3 Answers3

23

you could use the WC_Cart set_quantity method

And do as this in your php:

$cart = WC()->instance()->cart;
$id = $_POST['product_id'];
$cart_id = $cart->generate_cart_id($id);
$cart_item_id = $cart->find_product_in_cart($cart_id);

if($cart_item_id){
   $cart->set_quantity($cart_item_id,0);
}
Greg Berger
  • 714
  • 7
  • 10
  • 4
    Just note, that this code won't work for a variable product. In this case you will ether have to add the `$variation_id` to the function `generate_cart_id()` or you pass the `$cart_id` (actually it's correctly called `$cart_item_key` from the ajax script already. – rassoh May 23 '15 at 14:30
  • 1
    I can't seem to get it to work for a variable product. I've passed along my variation ID as a parameter in generate_cart_id, as the docs state, but nothing happens. Am I forgetting something? – Paul M Aug 30 '15 at 15:55
  • Can you please clear here where to put this function ? – Lipsa Jan 06 '16 at 11:41
  • You should add that code in the php called in ajax. I must say that I'm not sure this still works with recent versions of woo commerce – Greg Berger Jan 06 '16 at 21:58
  • Anyone could implement this to variable product? http://stackoverflow.com/questions/39286294/remove-variable-product-from-cart-with-ajax-in-woocommerce – cutez7boyz Sep 03 '16 at 02:14
6

use this :

$cart = $woocommerce->cart;

foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item){
    if($cart_item['product_id'] == $_POST['product_id'] ){
        // Remove product in the cart using  cart_item_key.
        $cart->remove_cart_item($cart_item_key);
    }
}
Community
  • 1
  • 1
user6172224
  • 61
  • 1
  • 1
1

Try this one :

foreach ( $woocommerce->cart->cart_contents as $cart_item_key => $cart_item ) {

 if($cart_item['product_id'] == $product_id){

  unset($cartdetails->cart_contents[$cart_item_key]);

 }
}
Mamoun Benghezal
  • 5,264
  • 7
  • 28
  • 33
joseph
  • 11
  • 1