Search code examples
phpajaxwordpresswoocommercecoupon

Ajax remove coupon from mini-cart WooCommerce


I am using the mini-cart.php file and the cart widget to show cart content and I am using AJAX add to cart standard built in WooCommerce. Adding products to my cart works fine. I added an area to show if any coupon is added (see my code). Works all fine but when deleting the coupon I want it to be done without loading a next page. It only needs to update the mini-cart.php. Can someone help me? Basically I need some function or something which updates mini-cart.php without reloading the page.

<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
    <p>Actieve kortingscode: <?php echo esc_attr( sanitize_title( $code ) ); ?></p>
    <p><?php wc_cart_totals_coupon_html( $coupon ); ?></p>
<?php endforeach; ?>

Solution

  • You will find the answer to your question in WooCommerce documentation

    https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html

    The short answer is you need to make a call to WC()->cart->remove_coupons();

    As of the "how", check this SO answer (different thing but same way to resolve it)

    https://stackoverflow.com/a/41593818/10864482