Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce programmatically deleting from cart

How do I programmatically remove an item from the Woocommerce cart using AJAX? I tried putting a function in my functions.php file and accessing that but nothing gets deleted. I tried hard-coding product 299 but it doesn't delete. Here's what I did:

functions.php

function remove_item_from_cart() {
    $cart = WC()->instance()->cart;
    $id = 299;
    $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);
    }
    return true;
}

themes/mine/main.js

$.ajax({
    type: 'POST',
    dataType: 'text',
    url: "http://www.../wp/wp-content/themes/mine/functions.php",
    data: {
        action: 'remove_item_from_cart'
    },
    success: function( data ) {
        console.log(data);
    }
});
like image 396
timpiele Avatar asked Feb 22 '16 19:02

timpiele


People also ask

How do I remove items from my cart in WooCommerce?

In order to remove a specific item from your WooCommerce cart, you must loop through the cart, get the item key and wrap the remove_cart_item() function with a product ID dependant if statement, as per the below example. Note, you can replace WC() with $woocommerce.

How do I remove the add to cart icon in WooCommerce?

And with a plugin like WooCommerce, the right way to remove the add to cart button was using a simple filter. The above code is run before any other functionality is checked. If the filter is_purchasable is set to true for the product, the 'Add to Cart' button is displayed, else the button is hidden.


2 Answers

For WooCommerce 3.0+ you can do it using the built-in remove_cart_item() function

function findCartItemKey($cartItems, $productId){               
  foreach($cartItems as $cartKey => $item){
    $product = $item['data'];
    if($product->get_id() == $productId){
            return $cartKey;
    }       
    return false;
  }    
}

global $woocommerce;
$cartItemKey = findCartItemKey($woocommerce->cart->get_cart())
$woocommerce->cart->remove_cart_item($cartItemKey);
like image 64
Akshay Agarwal Avatar answered Oct 30 '22 16:10

Akshay Agarwal


Use proper ajax method of wordpress like this: This worked fine for me.

//functions.php

    function remove_item_from_cart() {
    $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);
       return true;
    } 
    return false;
    }

    add_action('wp_ajax_remove_item_from_cart', 'remove_item_from_cart');
    add_action('wp_ajax_nopriv_remove_item_from_cart', 'remove_item_from_cart');

//main.js

    $.ajax({
        type: "POST",
        url: 'http://localhost/your_site/wp-admin/admin-ajax.php',
        data: {action : 'remove_item_from_cart','product_id' : '4'},
        success: function (res) {
            if (res) {
                alert('Removed Successfully');
            }
        }
    });
like image 29
Maha Dev Avatar answered Oct 30 '22 14:10

Maha Dev