Search code examples
phptemplateswoocommercehook-woocommercecart

Remove Totals section from WooCommerce Cart while keeping Subtotal line


I've found some code from here: How to remove order total from cart and checkout page woocommerce

that will let me remove the entire 'Cart Totals' block from my cart page:

//removal of 'Total' block on cart page
add_action( 'woocommerce_cart_collaterals', 'remove_cart_totals', 9 );
function remove_cart_totals(){
    // Remove cart totals block
    remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cart_totals', 10 );

    // Add back "Proceed to checkout" button (and hooks)
    echo '<div class="cart_totals">';
    do_action( 'woocommerce_before_cart_totals' );

    echo '<div class="wc-proceed-to-checkout">';
    do_action( 'woocommerce_proceed_to_checkout' );
    echo '</div>';

    do_action( 'woocommerce_after_cart_totals' );
    echo '</div><br clear="all">';
}

It works, however, Id like to still keep the subtotal visible. basically, just remove the 'Cart totals' and total section. Is this possible?

Picture of cart page

I tried the above code, and it worked to get rid of everything in the totals block, but I want to try and keep the subtotal.


Solution

  • To keep the cart subtotal display while removing all other totals, use the following:

    add_action( 'woocommerce_cart_collaterals', 'keep_only_cart_subtotal', 9 );
    function keep_only_cart_subtotal(){
        // Remove cart totals block
        remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cart_totals', 10 );
    
        // Add back Subtotal, "Proceed to checkout" button (and hooks)
        printf('<div class="cart_totals%s">', ( WC()->customer->has_calculated_shipping() ) ? ' calculated_shipping' : '');
    
        do_action( 'woocommerce_before_cart_totals' );
        
        ?>
        <table cellspacing="0" class="shop_table shop_table_responsive">
        <tr class="cart-subtotal">
            <th><?php esc_html_e( 'Subtotal', 'woocommerce' ); ?></th>
            <td data-title="<?php esc_attr_e( 'Subtotal', 'woocommerce' ); ?>"><?php wc_cart_totals_subtotal_html(); ?></td>
        </tr>
        </table>
        <?php
    
        echo '<div class="wc-proceed-to-checkout">';
    
        do_action( 'woocommerce_proceed_to_checkout' );
    
        echo '</div>';
    
        do_action( 'woocommerce_after_cart_totals' );
    
        echo '</div><br clear="all">';
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and work.