Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce | Clear cart by Ajax

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();
});
like image 623
Kode Bryant Avatar asked Oct 21 '16 10:10

Kode Bryant


People also ask

What is AJAX Cart WooCommerce?

WooCommerce AJAX Cart is a WordPress Plugin that changes the default behavior of WooCommerte Cart Page, allowing a buyer to see the Total price calculation when change the Quantity of a product, without need to manually click on “Update cart” button. This improves the user experience when purchasing a product.

How to remove AJAX Add to Cart 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.

How to disable AJAX Cart fragments in WooCommerce?

Disable Cart Fragmentation Using a Plugin Once you've downloaded and installed the plugin, go to it's settings and scroll down to the Site Performance section. Then check the box next to Disable WooCommerce Cart Fragments and click the Save Changes button at the bottom.

How do I add a shopping cart to WooCommerce?

Simply go to the plugins page and search for the plugin you want. Once the plugin has been installed and activated, you need to get to the main settings page to configure the settings how you would like. To do this, click on WooCommerce > Menu Cart Setup.


1 Answers

Please change your php code to

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

Your parameter that is passed in is data and not productID

like image 135
MirzaP Avatar answered Oct 25 '22 05:10

MirzaP