Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove product in the cart using ajax in woocommerce

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();
}
like image 825
user3331550 Avatar asked Feb 20 '14 07:02

user3331550


People also ask

How do I remove items from my cart in WooCommerce?

You are using it like so: WC()->cart->remove_cart_item($product_3); Try this instead: WC()->cart->remove_cart_item($cart_item_key);

Does WooCommerce use Ajax?

The Ajax-Enabled, Layered Navigation extension for WooCommerce provides a richer user experience for your customer and is particularly well suited for stores that have a lot of variable products or products with a number of attributes.

How do I disable Ajax in WooCommerce?

Under WooCommerce > Settings > Products > General it's recommended to disable Ajax add to cart behavior and, if possible, to enable redirection to the Cart page. This will always force a page reload (and/or a redirect) and therefore will save the user an Ajax call needed to update the Cart on the go.


2 Answers

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);
}
like image 196
Greg Berger Avatar answered Sep 20 '22 15:09

Greg Berger


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);
    }
}
like image 37
user6172224 Avatar answered Sep 22 '22 15:09

user6172224