I realise there are duplicate threads, but the solutions posed aren't working (100%) - this is the solution I am working with https://stackoverflow.com/a/24027583/1826992. Basically the ajax call is removing the item from the cart but the mini-cart isn't updating until I refresh the page. Here's where I'm at:
add_action( 'wp_footer', 'add_js_to_wp_wcommerce');
function add_js_to_wp_wcommerce(){ ?>
<script type="text/javascript">
jQuery('body').on('click',".remove",function (){
var product_id = jQuery(this).attr("data-product_id");
console.log(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() {
$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);
}
}
As I mentioned this all seems to work up until "action". Console.log(data) at this point only carries "0". I assume this should have product_id.
Thanks in advance for any help.