show html on checkout page based on cart total woocommerce

  php, woocommerce, wordpress

I am adding "special" products to my cart on the checkout page, the products are only available once the cart value gets to 90 or above, I have the products hidden on page when the value is not correct, but I cannot get the products to how when the an item is added that raises the value of the cart to above 90. Here is my current ajax,

$(document).on('change', '.gift_select_qty', function(e){
    e.preventDefault();
    var $thisbutton = $(this),
        product_qty = $(this).val(),
        product_id = $(this).data('product_id');

        if(product_qty > 0) {
            var data = {
                action: 'woocommerce_ajax_add_to_cart',
                product_id: product_id,
                product_sku: '',
                quantity: product_qty,
            };

            var url = wc_add_to_cart_params.ajax_url
     
        } else {
            var data = {
                action: 'remove_item_from_cart',
                product_id: product_id,
                product_sku: '',
                quantity: product_qty
            };

            var url = wc_add_to_cart_params.ajax_url;
        }

        $(document.body).trigger('added_to_cart', [$thisbutton, data]);

        $.ajax({
            type: 'post',
            url: url,
            data: data,
            success: function (response) {

                if (response.error && response.product_url) {
                    window.location = response.product_url;
                    return;
                } else {
                    $(document.body).trigger('update_checkout', [response.fragments, response.cart_hash, $thisbutton]);
                }
            },
        });

    return false;
});

And the PHP that this calls,

 function woocommerce_ajax_add_to_cart() {

$product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['product_id']));
$quantity = empty($_POST['quantity']) ? 1 : wc_stock_amount($_POST['quantity']);
$variation_id = absint($_POST['variation_id']);
$passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
$product_status = get_post_status($product_id);

if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id) && 'publish' === $product_status) {

    do_action('woocommerce_ajax_added_to_cart', $product_id);

    if ('yes' === get_option('woocommerce_cart_redirect_after_add')) {
        wc_add_to_cart_message(array($product_id => $quantity), true);
    }
    WC_AJAX :: get_refreshed_fragments();

} else {

    $data = array(
        'error' => true,
        'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id));

    echo wp_send_json($data);
}

wp_die();

}

Is there a way I can get the cart total in the ajax response I haved tried hooking into some code like below, but it does not work.

    add_filter('woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment'); 
function woocommerce_header_add_to_cart_fragment( $fragments ) { 
   $fragments['cart_total'] = floatval( preg_replace( '#[^d.]#', '', WC()->cart->get_cart_total()));
   return $fragments;
}

Source: Ask PHP

LEAVE A COMMENT