woocommerce - Get Cart Total as number

前端 未结 6 816
难免孤独
难免孤独 2020-12-16 01:03

I want to get total price of cart in my woocommerce plugin.

I want to get it as a float number like so: 21.00 but I don\'t know how to get it. My code outputs weird

相关标签:
6条回答
  • 2020-12-16 01:26

    I have code like this and working perfect:

    if ( ! WC()->cart->prices_include_tax ) {
        $amount = WC()->cart->cart_contents_total;
    } else {
        $amount = WC()->cart->cart_contents_total + WC()->cart->tax_total;
    }
    

    good luck !

    0 讨论(0)
  • 2020-12-16 01:36

    In 2020 with Woocommerce 4+

    $total_cart = WC()->cart->get_displayed_subtotal(); // without taxs and shipping fees
    echo $total_cart; // ex: 0.00
    
    0 讨论(0)
  • 2020-12-16 01:40

    Just access the total property directly, it's public:

    global $woocommerce;
    echo $woocommerce->cart->total;
    
    0 讨论(0)
  • 2020-12-16 01:45
    global $woocommerce;
    $amount = $woocommerce->cart->cart_contents_total+$woocommerce->cart->tax_total;
    

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

    0 讨论(0)
  • 2020-12-16 01:45

    Try this WC()->cart->cart_contents_total

    0 讨论(0)
  • 2020-12-16 01:48
    global $woocommerce;
    $woocommerce->cart->cart_contents_total (Cart total)
    $woocommerce->cart->tax_total (tax total)
    $woocommerce->cart->shipping_total (shipping total)
    
    0 讨论(0)
提交回复
热议问题