Get woocommerce carts total amount
Asked Answered
R

13

33

I am trying to apply a discount to a carts total price, but I can only do it to the item base price and not the over all price. I Googled and came across this post in the wordpress stackoverflow:

$amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) ); The preg_replace eliminates everything but decimal characters and colons.

Should you care to do math with it, the floatval converts the value from a string to a numeric one.

I tried adding:

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

and changing

$discount = round( (($discounting_amount / 100 ) *  $this->amount)*-1, WC()->cart->dp);

to

$discount = round( (($discounting_amount / 100 ) *  $amount2)*-1, WC()->cart->dp);

But I get the following error:

Fatal error: Call to a member function get_cart_total() on a non-object in...
Radiophone answered 7/3, 2014 at 12:4 Comment(0)
T
36

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.

Translucent answered 7/3, 2014 at 13:4 Comment(2)
For some countries you have to consider the comma too, try this '#[^\d.,]#'Primipara
WC()->cart->get_cart_total() did the trick without having to call the global $woocommerceCater
V
40

Try this:

WC()->cart->cart_contents_total

The function get_cart_total uses wc_price function thas converts cart_contents_total to currency.

Varicose answered 26/5, 2014 at 12:18 Comment(1)
Use WC()->cart->get_cart_contents_total(); to get Float from "cart_contents_total" property.Boyhood
T
36

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.

Translucent answered 7/3, 2014 at 13:4 Comment(2)
For some countries you have to consider the comma too, try this '#[^\d.,]#'Primipara
WC()->cart->get_cart_total() did the trick without having to call the global $woocommerceCater
K
23

This also work nicelly.

WC()->cart->total

Kalsomine answered 1/10, 2015 at 20:14 Comment(2)
This automatically includes the shippingYestreen
Also, it works if someone wants to get the cart total only in number (without HTML).Fisticuffs
M
20

As of late 2018, the best way is to use get_cart_contents_total(). This is the total of items in the cart after discounts.

WC()->cart->get_cart_contents_total(); // Float

Other methods are available for more specific needs, just have a look at the docs.

Micheal answered 29/10, 2018 at 19:3 Comment(1)
Yep this is a good one, like you said, after discounts and returns float. Thanks.Daze
S
9

This works perfectly and removes currency symbol:

     $woocommerce->cart->total;
Safeconduct answered 26/7, 2014 at 9:22 Comment(0)
I
8
global $woocommerce;
    $amount = $woocommerce->cart->cart_contents_total+$woocommerce->cart->tax_total;

You can also convert $amount in float value as per your requirement.

Immerge answered 13/4, 2016 at 5:58 Comment(0)
M
6

REPUESTA FINAL

global $woocommerce;
  
$order = wc_get_order( $order_id );
  
if ( $order ) {
$order->get_id();
$order->get_order_key();
$order->get_formatted_order_total();
$order->get_cart_tax();
$order->get_currency();
$order->get_discount_tax();
$order->get_discount_to_display();
$order->get_discount_total();
$order->get_fees();
$order->get_formatted_line_subtotal();
$order->get_shipping_tax();
$order->get_shipping_total();
$order->get_subtotal();
$order->get_subtotal_to_display();
$order->get_tax_location();
$order->get_tax_totals();
$order->get_taxes();
$order->get_total();
$order->get_total_discount();
$order->get_total_tax();
$order->get_total_refunded();
$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();
$order->get_item_count_refunded();
$order->get_total_qty_refunded();
$order->get_qty_refunded_for_item();
$order->get_total_refunded_for_item();
$order->get_tax_refunded_for_item();
$order->get_total_tax_refunded_by_rate_id();
$order->get_remaining_refund_amount();
  
// recorder ITEMs 

foreach ( $order->get_items() as $item_id => $item ) {
   $product_id = $item->get_product_id();
   $variation_id = $item->get_variation_id();
   $product = $item->get_product();
   $name = $item->get_name();
   $quantity = $item->get_quantity();
   $subtotal = $item->get_subtotal();
   $total = $item->get_total();
   $tax = $item->get_subtotal_tax();
   $taxclass = $item->get_tax_class();
   $taxstat = $item->get_tax_status();
   $allmeta = $item->get_meta_data();
   $somemeta = $item->get_meta( '_whatever', true );
   $type = $item->get_type();
}

$order->get_items_key();
$order->get_items_tax_classes();
$order->get_item_count();
$order->get_item_total();
$order->get_downloadable_items();
$order->get_line_subtotal();
$order->get_line_tax();
$order->get_line_total();
$order->get_shipping_method();
$order->get_shipping_methods();
$order->get_shipping_to_display();
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();
$order->get_customer_id();
$order->get_user_id();
$order->get_user();
$order->get_customer_ip_address();
$order->get_customer_user_agent();
$order->get_created_via();
$order->get_customer_note();
$order->get_address_prop();
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_company();
$order->get_billing_address_1();
$order->get_billing_address_2();
$order->get_billing_city();
$order->get_billing_state();
$order->get_billing_postcode();
$order->get_billing_country();
$order->get_billing_email();
$order->get_billing_phone();
$order->get_shipping_first_name();
$order->get_shipping_last_name();
$order->get_shipping_company();
$order->get_shipping_address_1();
$order->get_shipping_address_2();
$order->get_shipping_city();
$order->get_shipping_state();
$order->get_shipping_postcode();
$order->get_shipping_country();
$order->get_address();
$order->get_shipping_address_map_url();
$order->get_formatted_billing_full_name();
$order->get_formatted_shipping_full_name();
$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();
  
// Get Order Payment Details
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();
  
// Get Order URLs
$order->get_checkout_payment_url();
$order->get_checkout_order_received_url();
$order->get_cancel_order_url();
$order->get_cancel_order_url_raw();
$order->get_cancel_endpoint();
$order->get_view_order_url();
$order->get_edit_order_url();
$order->get_status();

}

// para obtener el simbolo '$'

get_woocommerce_currency_symbol()
Mattox answered 2/7, 2020 at 7:41 Comment(0)
S
4

To show the carts total including tax and discounts use this

$ordertotal = wp_kses_data( WC()->cart->get_total() );
Smorgasbord answered 25/5, 2016 at 0:37 Comment(0)
T
2
WC()->cart->subtotal; 
this function use for get total without currency
WC()->cart->get_total();
this function use for get total with currency
Tragus answered 14/9, 2020 at 6:20 Comment(0)
F
1

Function get_cart_contents_total() gives the total of items in the cart, but after discounts. Depending on the tax settings you might have to add taxes. Something lie this:

$cart_total_price = wc_prices_include_tax() ? WC()->cart->get_cart_contents_total() + WC()->cart->get_cart_contents_tax() : WC()->cart->get_cart_contents_total();
Froma answered 12/5, 2020 at 8:58 Comment(0)
S
1

The following code outputs formatted price with currency

wc_price( WC()->cart->cart_contents_total); // Output example $118,000
Sapanwood answered 26/7, 2020 at 18:28 Comment(0)
C
0
$totalamount = $woocommerce->cart->cart_contents_total; 

echo $totalamount;
Causeuse answered 16/8, 2018 at 12:19 Comment(1)
You should provide a bit detailed explanation for your code snippet.Knothole
S
0

From Docs

WC_Cart::get_cart_contents_total() – Gets cart total. This is the total of items in the cart, but after discounts. Subtotal is before discounts.

So, if you want with discounts, you may call

WC()->cart->get_cart_contents_total()

If you want without the discount, you may call

WC()->cart->get_subtotal()

There is another useful function for getting shipping

WC()->cart->get_shipping_total()

So, the total amount should be

WC()->cart->get_cart_contents_total() + WC()->cart->get_shipping_total()

Also you may also want to include tax in the calculation as pointed out by @geza-goz

Schauer answered 13/10, 2023 at 6:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.