woocommerce - 获取购物车总数作为数字

时间:2015-05-05 21:00:32

标签: php wordpress woocommerce

我想在我的woocommerce插件中获得购物车的总价。

我想把它作为一个浮点数来得到它:21.00但我不知道如何得到它。我的代码输出了奇怪的结果,这是我的确切代码:

$total         = $woocommerce->cart->get_total();
$total_a       = WC()->cart->get_total();
$total1        = $woocommerce->cart->get_total_ex_tax();
$total1_a      = WC()->cart->get_total_ex_tax();
$total2        = $woocommerce->cart->get_cart_total();
$total2_a      = WC()->cart->get_cart_total();

输出:

0,00 €
0,00 €
0,00 €
0,00 €
21,00 €
21,00 €

如果我从string转换为float,结果当然是0.00。

如何以浮点数的形式获得购物车总数的任何帮助?

9 个答案:

答案 0 :(得分:6)

直接访问total属性,它是公开的:

global $woocommerce;
echo $woocommerce->cart->total;

答案 1 :(得分:4)

package lexical

import scala.language.postfixOps

import scala.util.parsing.combinator.lexical.Lexical
import scala.util.parsing.input.CharSequenceReader._

/**
 * Created by Marin on 28/03/16.
*/
class MyLexical extends Lexical with MyTokens {

def token: Parser[Token] = (
    //procDef                                   ^^ { case first ~ chars => processNewProcedure(chars  mkString "") }
  word2 ^^ { case rest => {

      /*val s = if (second.isEmpty) "" else second mkString ""
      val t = if(third.isEmpty) "" else  third mkString ""
      val f = if(fourth.isEmpty) "" else fourth mkString ""

      StringLit(s"$first$s$t$f$rest")*/

      println(rest)
      StringLit("Smth")
  }
  }
  | formalChar ~ rep(identChar | digit)       ^^ { case first ~ rest => Formal(first :: rest mkString "") }
  | identChar ~ rep(identChar | digit)        ^^ { case first ~ rest => processIdent(first :: rest mkString "") }
  | procDigit                                 ^^ { case first ~ second ~ rest => NumericLit((first mkString "") :: second.getOrElse("") :: rest mkString "") }
  | '\"' ~ rep(chrExcept('\"', EofCh)) ~ ' '  ^^ { case '\"' ~ chars ~ ' ' => StringLit(chars mkString "") }
  | EofCh                                     ^^^ EOF
  | delim
  | failure("Illegal character")
  )

def processNewProcedure(chars: String) =
    if(reserved.contains(chars)) throw new RuntimeException
    else {
        Identifier(chars)
    }

def procDef = toSeq ~> identChar ~ rep(identChar | elem('_')) <~ formalChar.* <~ endSeq

def toSeq = 't' ~ 'o' ^^^ "to"
def endSeq = 'e' ~ 'n' ~ 'd' ^^^ "end"

def processIdent(name: String) = {

    if (reserved contains name) {
        Keyword(name)
    } else {
        Identifier(name)
    }
}

def word = {

    '[' ~ ((whitespaceChar | digit)*) ~ (_delim | identChar) ~ rep(whitespaceChar | digit) ~ ']'
}

def word2 = {

    //'[' ~> rep(whitespaceChar | digit) ~> rep(_delim | identChar) <~ rep(whitespaceChar | digit) <~ ']'
    //'[' ~ rep(chrExcept('[', ']')) ~ ']'

    rep1('[') ~ rep1(chrExcept('[', ']') | digit) ~ rep(_delim) ~ rep1(']')

    //rep1('[') ~ identChar ~ rep(']') ~ rep('+') ~ rep1(']')
    //'[' ~ (_delim | chrExcept('[', ']')) ~ ']'
}

def word3 = {

    '[' ~> rep(digit | letter | _delim) <~ ']'
}

def procDigit = digit.+ ~ '.'.? ~ digit.*

def identChar = letter | elem('_')

def formalChar =  ':' ~ identChar

override def whitespace: Parser[Any] = rep[Any] (
    whitespaceChar
    | ';' ~ comment
)

def comment: Parser[Any] = rep(chrExcept(EofCh, ';')) ^^ { case _ => ' ' }


/****** Pure copy-paste ******/

/** The set of reserved identifiers: these will be returned as `Keyword`s. */
val reserved = new scala.collection.mutable.HashSet[String]

/** The set of delimiters (ordering does not matter). */
val delimiters = new scala.collection.mutable.HashSet[String]

private lazy val _delim: Parser[Token] = {
    // construct parser for delimiters by |'ing together the parsers for the individual delimiters,
    // starting with the longest one -- otherwise a delimiter D will never be matched if there is
    // another delimiter that is a prefix of D
    def parseDelim(s: String): Parser[Token] = accept(s.toList) ^^ { x => Keyword(s) }

    val d = new Array[String](delimiters.size)
    delimiters.copyToArray(d, 0)
    scala.util.Sorting.quickSort(d)
    (d.toList map parseDelim).foldRight(failure("no matching delimiter"): Parser[Token])((x, y) => y | x)
}
protected def delim: Parser[Token] = _delim
}

您还可以根据您的要求在浮动值中转换$ amount。

答案 2 :(得分:3)

我有这样的代码并且工作得很完美:

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

答案 3 :(得分:1)

尝试一下 WC()->购物车-> cart_contents_total

答案 4 :(得分:1)

在2020年推出Woocommerce 4 +

$total_cart = WC()->cart->get_displayed_subtotal(); // without taxs and shipping fees
echo $total_cart; // ex: 0.00

答案 5 :(得分:1)

最好的方法是使用 get_total(),同时传递默认值“view”以外的上下文。当上下文被查看时,价格将被格式化以供显示。当设置为其他任何值时,它将传回原始值。

示例:

WC()->cart->get_total( 'raw' );

还值得注意的是,$woocommerce(当然前提是您访问了全局第一个)与WC() 完全相同。我建议尽可能使用 WC()

答案 6 :(得分:0)

global $woocommerce;
$woocommerce->cart->cart_contents_total (Cart total)
$woocommerce->cart->tax_total (tax total)
$woocommerce->cart->shipping_total (shipping total)

答案 7 :(得分:0)

它有一些方法,您不需要从属性中获取它们。

使用:$.ajax( { type:'GET', url:'api', dataType: 'json', success: function(data){ console.log(data); for (var i=0; i<data.length; i++) { var row = $( '<tr>' + '<td>' + data[i].id + '</td>' + '<td>' + data[i].name + '</td>' + '<td>' + ' <button onclick="editItem(ID)">EDIT</button>' + ' <button onclick="deleteItem(ID)">DELETE</button>' + '</td>' + '</tr>' ); $('#my_table').append(row); } } } );

代替:WC()->cart->get_cart_contents_total()


并使用:WC()->cart->cart_contents_total

代替:WC()->cart->get_taxes_total()

答案 8 :(得分:0)

Woocommerce 4.8 WC()->cart->total 工作正常,虽然我有点担心在没有 getter 帮助的情况下获得这个值,但它现在似乎是最简单的方法。