I have read like every single topic about woocommerce but cannot find out how to add a minus button which decreases the quantity of a cart item (with ajax preferably) in the Woocommerce MiniCart.
I did manage to add a button which increases the quantity using the woocommerce shortcode and via several other code samples. However, I cannot find how to decrease the quantity anywhere.
There are multiple questions about this, none answered. Or I`m searhing in the wrong directory.
However, can someone give me a code example on how to decrease cart`s quantity for a product out of the box? I already tried a custom php file with these lines of code:
$cartKey = $_POST['cart_item_key']; //The cart key required by set_quantity method
$cartQty = $_POST['cart_item_qty']; //the quantity I provide in my post
global $woocommerce;
echo $woocommerce->cart->set_quantity($cartKey,$cartQty);
But calling this via AJAX post gives me an error (internal server error). I also tried add to cart with negative quantity, did not work either.
UPDATE: adding this code to a WP page template and calling that page does not give me the error anymore. However, after calling the code, it does not update the cart.
What can I do? Thanks a lot I hope someone can help here!
Ok, so I figured this out. What I did:
1: Create a WP page responsible for handling the AJAX update cart. I think you can do this with just a simple PHP file too, but I just created a custom WP template for now. So I added a page template called ‘template-setquantity.php’ with the following contents:
Create a wordpress backend page using this template. For this tutorial the url should be: updatecart
Now copy over the mini-cart.php from woocommerce to your custom theme. So I created the dir woocommerce/cart in my custom theme and copied the file mini-cart.php into this folder
Update the mini-cart.php template to add your plus and minus buttons to update quantity in mini-cart. For me it meant adding the following code, somewhere in the loop of each product:
So now we have setup the page which catches requests to update quantity and we have added or plus and minus button to each product in the mini cart. As final step we need to create a JS function which does 2 things: AJAX call to our updatecart template page and AJAX call to refresh the cart contents visually 1.
In my case, I added these functions to the footer. As you have read in step 4, the function of the minus buttons for onClick is updateQty, so that has to be our JS function name. Paste the following JS code to for example your footer (please note I use Jquery):
I hope this helps!
NOTE that in later versions of Woocommerce I would be better to replace the global $woocommerce with function WC() (http://docs.woothemes.com/wc-apidocs/function-WC.html)