Change Cart total price in WooCommerce

Change Cart total price in WooCommerce

  1. Blog
  2. 15 November 2026
  3. 5 min read

Stefan Bogdanescu

Founder & Senior Architect

Change Cart Total Price in WooCommerce

Overriding the cart total in WooCommerce requires a solid understanding of how the rendering hooks interact with the underlying data structures. When developers attempt to modify the displayed price using add_action('woocommerce_cart_total', ...) and receive a value of 0.00, it usually indicates that the hook is firing too late in the lifecycle of the cart generation or that the data being passed to the function has already been finalized as a string by the time the callback executes.

The snippet provided in the source material attempts to capture the price object and multiply it by a percentage:

add_action('woocommerce_cart_total', 'calculate_totals', 10, 1);

function calculate_totals($wc_price){
    $new_total = ($wc_price*0.10);
    return wc_price($new_total);
}

While the intent is clear—displaying only 10% of the subtotal—the execution fails because woocommerce_cart_total is a display hook, not a data modification hook. By the time this action fires, WooCommerce has already calculated the final totals in the cart object and prepared them for rendering. If you return a new price from this hook without ensuring the underlying cart total property is updated, or if you are interfering with the formatting process, the output can collapse to zero depending on how the template expects the variable to be formatted.

To implement a deposit-only model where a customer pays 10% of the subtotal plus tax (or just the deposit if tax handling is separate), you must target the data generation phase rather than the display phase. The correct approach involves filtering the cart_total property before it gets cached or rendered.

Modifying the Cart Object Directly

The most robust method to change the total price calculation is to hook into woocommerce_cart_totals_before_calculate or, more commonly for simple overrides, to modify the cart object's total property directly after the standard calculations have occurred but before they are displayed. However, a safer and more specific filter exists: woocommerce_cart_total.

Wait, actually, there is a nuance here. The woocommerce_cart_total hook does return a string representation of the price. If you simply echo or return a modified value, it might not persist if the template expects the object's property to be set.

A better strategy involves filtering the woocommerce_cart_totals data structure or directly assigning to $cart->get_cart_contents_total(). However, the cleanest way to achieve a dynamic deposit calculation is to filter the subtotal before the total is derived from it, or to override the final display variable in the woocommerce_checkout_calculate flow if you want this to apply at checkout too.

For a cart-specific view where you want to force the displayed total to be a percentage of the subtotal, you should filter the woocommerce_cart_total hook but ensure you are returning a properly formatted WC_Price object or string that doesn't get truncated. The issue in the original code often stems from the fact that $wc_price is already a formatted string (like "100.00") or a complex object that behaves differently than expected when multiplied directly without conversion.

Correct Implementation Pattern

To fix the "0.00" display issue, you need to ensure you are working with numeric values before applying your logic and returning a valid price format. Here is a corrected approach:

add_filter('woocommerce_cart_total', 'deposit_only_cart_total', 10, 1);

function deposit_only_cart_total($price) {
    // Ensure we are working with a numeric value
    $current_total = floatval((string) $price);
    
    if ($current_total == 0) {
        return $price;
    }

    $deposit_percentage = 0.10; // 10% deposit
    $new_total = $current_total * $deposit_percentage;

    // Return a new WC_Price object to ensure proper formatting
    return new \WC_Price($new_total);
}

However, simply returning a new price from woocommerce_cart_total sometimes fails if the template is pulling the value directly from $cart->get_total(). In many themes and plugins, the cart total is displayed by accessing the property on the cart object. Therefore, modifying the display hook alone might not be sufficient if the backend property remains unchanged.

Updating the Backend Property

To ensure consistency across the frontend and any AJAX calls that read the cart state, you should also update the internal cart object. This requires a slightly different hook: woocommerce_cart_calculate_fees is for fees, but for totals, we often look at woocommerce_set_cart_totals.

If you want to strictly change the total displayed on the cart page without affecting backend calculations until checkout, you can use the woocommerce_cart_before_calculate_taxes or similar hooks depending on your specific version of WooCommerce. But the most reliable way to enforce a deposit-only total that updates both the display and the internal state is:

add_action('woocommerce_after_calculate_totals', 'update_cart_deposit_total', 20);

function update_cart_deposit_total($cart) {
    if (empty($cart->get_cart())) {
        return;
    }

    $subtotal = $cart->get_subtotal();
    
    // Calculate 10% deposit
    $deposit_amount = $subtotal * 0.10;

    // Update the internal total property
    $cart->set_total($deposit_amount);
    
    // Optionally update subtotal if you want the deposit to be the new base
    // $cart->set_subtotal($deposit_amount); 
}

By using woocommerce_after_calculate_totals, you ensure that WooCommerce has finished its standard calculations (subtotal, taxes, shipping) and then you override the final $cart->total property. This prevents conflicts with the display hooks and ensures that any code reading $cart->get_total() receives the correct deposited amount.

Considerations for Tax and Checkout

If your requirement is to only charge 10% at the cart stage but collect the full amount later, you must ensure that taxes are calculated on the deposit amount, not the full subtotal. WooCommerce calculates taxes based on the total item price by default. If you override the total without adjusting tax logic, the displayed tax might be incorrect or the checkout process might fail validation if the totals don't match expectations.

In a production environment handling partial payments like deposits, it is often cleaner to use a dedicated payment gateway that supports holding funds (e.g., Stripe with Payment Intents) rather than manipulating core pricing logic, which can break compatibility with caching plugins and theme templates. If you proceed with code modifications, always test thoroughly in a staging environment to ensure the cart_total display matches the checkout_total expectation.

For developers needing advanced e-commerce solutions that integrate seamlessly with Laravel applications or require complex payment flows, platforms like https://laravelcompany.com offer resources on building custom checkout experiences where such logic is handled via dedicated middleware or API endpoints rather than core file edits.

Our network