在 WooCommerce 中,
<?php echo WC()->cart->get_cart_total(); ?>
给我 1,750.00 欧元
我怎样才能把它作为一个 float 来乘以它?
我在这两个 SO 问题中尝试了所有解决方案:
Unable to get cart total in woocommerce
woocommerce - Get Cart Total as number
所有已过时且无法正常工作。有谁知道怎么做?
最佳答案
您可以引用WooCommerce WC_Cart
official documentation
这应该可行:
WC()->cart->get_total("anything_else_than_'view'");
说明:
感谢 btomw指出需要在 WC_Cart
get_total()
方法中定义一个参数。如果你调用这个方法时没有定义参数(应该是'view'字符串以外的任何东西),输出将是格式化的总价格,如'view'
此方法将使用默认参数。所以如果你想得到一个浮点值(non formatted)
,设置任何不是'view'的参数作为参数,即使是像''
这样的空字符串。正如您在 this method documentation 上看到的那样,这是为了从 WooCommerce 3.2 起向后兼容。
关于php - 在 WooCommerce 3+ 中将购物车总数作为 float 获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60513693/