I'm trying to set up an AJAX function to clear my cart

**HTML**

```
<a onclick="clearCart(this)" data-href="/product-page/" data-productID="182">Go to Product</a>

```

**JavaScript**

```
function clearCart(d) {
    jQuery(document).ready(function($) {
        var productID = d.getAttribute("data-productID");
        $.ajax({
            url: "addtocart.php",
            data: {productID: productID},
            type: "post",
            success: function(output) {
                window.location = d.getAttribute("data-href");
                //alert(output);
            }
        });
    });
}

```

**PHP**

```
if(isset($_POST['productID']) && !empty($_POST['productID'])) {   
    global $woocommerce;
    $woocommerce->cart->empty_cart();
    //echo $_POST['productID'];
}

```

**Result**

* Internal Server Error caused by 3rd PHP line
* Alerting var output is working (check outcommented code)

**SOLUTION**

I figured it out by myself and some great help from @MirzaP

JS

```
      function clearCart(d) {
            jQuery.post(
                "https://dercampus.ch/wp-admin/admin-ajax.php", 
                //ajaxurl, 
                {
                    "action": "clearcart",
                    "data":   d.getAttribute("data-productid")
                }, 
                function(){
                    window.location = d.getAttribute("data-href");
                }
            );
        }

```

PHP

```
add_action('wp_ajax_nopriv_clearcart',function(){
    global $woocommerce;
    $woocommerce->cart->empty_cart();
});

```