Change Cart Total Price in Woocommerce

Change Cart total price in WooCommerce

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.

How to use cart total to change cart items prices in WooCommerce

You can simply not use cart total in woocommerce_before_calculate_totals when you want to alter cart item price for many reasons…

Instead you will get cart item subtotal inside woocommerce_before_calculate_totals hook. On the code below I use the discounted cart item subtotal including taxes:

add_action('woocommerce_before_calculate_totals', 'customize_cart_item_prices');
function customize_cart_item_prices( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

// Avoiding the hook repetition for price calculations
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;

$threshold_amount = 1000; // Min subtotal
$discount_rate = 0.2; // price discount rate (20%)

$cart_items = $cart->get_cart();

// Getting non discounted cart items subtotal
$subtotal_excl_tax = array_sum( wp_list_pluck( $cart_items, 'line_subtotal' ) );
$subtotal_tax = array_sum( wp_list_pluck( $cart_items, 'line_subtotal_tax' ) );

// Getting discounted cart items subtotal
$total_excl_tax = array_sum( wp_list_pluck( $cart_items, 'line_total' ) );
$total_tax = array_sum( wp_list_pluck( $cart_items, 'line_tax' ) );

if( ( $total_excl_tax + $total_tax ) >= $threshold_amount ) {
// Loop through cart items
foreach ( $cart_items as $item ) {
$price = $item['data']->get_price(); // Get price

$item['data']->set_price( $price * ( 1 - $discount_rate ) ); // Set new price
}
}
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

See on Change cart item prices in Woocommerce 3 answer code, to see how to handle minicart displayed custom cart item price.

Get woocommerce carts total amount

You need to call the global variable to ensure that it gets the correct values.

If you add

 global $woocommerce;

just before

 $amount2 = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );

that should solve your problem.

Cart total price Override in WooCommerce

You're not accumulating the subtotal in your woocommerce_calculate_totals function. You're writing $cart_sub_total = $final_price; every time so you'll only have the price of the last item at the end. Instead it should be $cart_sub_total = $cart_sub_total + $final_price;.

Issue with changing WooCommerce cart item prices based on product meta

The woocommerce_cart_item_name hook is being misused in your code attempt. A filter hook should always return something compared to the use of echo.

You should use the woocommerce_before_calculate_totals hook instead

So you get:

function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

// Change price if > 3000
$threshold = 3000;

// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Get regular price
$price = $cart_item['data']->get_regular_price();

// Subtotal = Get quantity * price
$subtotal = $cart_item['quantity'] * $price;

// Greater than
if ( $subtotal > $threshold ) {
// Get meta
$meta = $cart_item['data']->get_meta( '_opt_price_var', true );

// NOT empty
if ( ! empty ( $meta ) ) {
// Set new price
$cart_item['data']->set_price( $meta );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );


Related Topics



Leave a reply



Submit