mardi 28 juin 2016

WooCommerce - Calculate cart total after Ajax update

I have a small bit of code which calculates the cart total (excluding taxes) and outputs a Free Shipping upsell, if the cart total is less than $50.

// Shipping Upsell
/**
 * Checks the cart for the Total excluding taxes
 * @param $total_required
 * @return bool
 */
function qualifies_basedon_cart_total( $total_required ) {
    /*
     * We only want to run this on the cart or checkout page
     * If the cart subtotal is less than $total_required for free shipping, return true
     */
if( is_cart() || is_checkout () ) {
    if( WC()->cart->subtotal_ex_tax < $total_required ) {
        return true;
    }
}
// do nothing
return false;
}

function shipup(){
    // tests if total is below $50 and displays upsell if query returns ture
    if( qualifies_basedon_cart_total( 50 ) ) {
    echo "<div class =\"shipup\"><h3>Free Shipping</h3>\n<p>Get free shipping on all orders over $50!</p></div>";
    }
}

add_action ('woocommerce_cart_collaterals','shipup', 1);

The code above works great on the initial page load, but after changing the quantity on the cart page and selecting 'Update Cart' the code I have above (in functions.php) does not adjust itself based on the new cart total. enter image description here

I believe the update cart button uses AJAX and my code is not able to tap into that. How can AJAXIFY my code so that it works based on the dynamic cart total?



via Chebli Mohamed

1 commentaire:

  1. Looking for woocommerce Rest API Integration for your online store? Hire our developers & programmers for your custom API and extensions. +91-9806724185 or Contact@expresstechsoftwares.com

    RépondreSupprimer