Change Cart total price in WooCommerce
This does not answer this question. Loic's does. This is another way of doing it to show a line item of 10% off:
function prefix_add_discount_line( $cart ) {
$discount = $cart->subtotal * 0.1;
$cart->add_fee( __( 'Down Payment', 'yourtext-domain' ) , -$discount );
}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' );
Since Woocommerce 3.2+ it does not work anymore with the new Class
WC_Cart_Totals
...New answer: Change Cart total using Hooks in Woocommerce 3.2+
First woocommerce_cart_total
hook is a filter hook, not an action hook. Also as wc_price
argument in woocommerce_cart_total
is the formatted price, you will not be able to increase it by 10%. That's why it returns zero.
Before Woocommerce v3.2 it works as some
WC_Cart
properties can be accessed directly
You should better use a custom function hooked in woocommerce_calculate_totals
action hook
this way:
// Tested and works for WooCommerce versions 2.6.x, 3.0.x and 3.1.x
add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );
function action_cart_calculate_totals( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( !WC()->cart->is_empty() ):
## Displayed subtotal (+10%)
// $cart_object->subtotal *= 1.1;
## Displayed TOTAL (+10%)
// $cart_object->total *= 1.1;
## Displayed TOTAL CART CONTENT (+10%)
$cart_object->cart_contents_total *= 1.1;
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Is also possible to use WC_cart
add_fee()
method in this hook, or use it separately like in Cristina answer.