29

我正在尝试对购物车总价应用折扣,但我只能对商品基价而不是总价进行折扣。我用谷歌搜索并在 wordpress stackoverflow 中看到了这篇文章:

$amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) ); preg_replace 消除了除十进制字符和冒号之外的所有内容。

如果您愿意用它来做数学运算,floatval 会将值从字符串转换为数字。

我尝试添加:

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

和改变

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

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

但我收到以下错误:

Fatal error: Call to a member function get_cart_total() on a non-object in...
4

12 回答 12

35

尝试这个:

WC()->cart->cart_contents_total

函数 get_cart_total 使用 wc_price 函数将 cart_contents_total 转换为货币。

于 2014-05-26T12:18:19.557 回答
34

您需要调用全局变量以确保它获得正确的值。

如果你添加

 global $woocommerce;

就在之前

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

那应该可以解决您的问题。

于 2014-03-07T13:04:48.437 回答
17

这也很好地工作。

WC()->cart->total

于 2015-10-01T20:14:32.477 回答
16

截至 2018 年底,最好的方法是使用get_cart_contents_total(). 这是折扣后购物车中的商品总数。

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

其他方法可用于更具体的需求,只需查看文档

于 2018-10-29T19:03:01.920 回答
8

这完美地工作并删除了货币符号:

     $woocommerce->cart->total;
于 2014-07-26T09:22:14.057 回答
7
global $woocommerce;
    $amount = $woocommerce->cart->cart_contents_total+$woocommerce->cart->tax_total;

您还可以根据您的要求将 $amount 转换为浮点值。

于 2016-04-13T05:58:32.793 回答
5

普埃斯塔决赛

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()
于 2020-07-02T07:41:23.100 回答
3

要显示包括税费和折扣在内的购物车总数,请使用此

$ordertotal = wp_kses_data( WC()->cart->get_total() );
于 2016-05-25T00:37:33.077 回答
2
WC()->cart->subtotal; 
this function use for get total without currency
WC()->cart->get_total();
this function use for get total with currency
于 2020-09-14T06:20:25.853 回答
1

以下代码输出带有货币的格式化价格

wc_price( WC()->cart->cart_contents_total); // Output example $118,000
于 2020-07-26T18:28:01.243 回答
0

函数 get_cart_contents_total() 给出购物车中的商品总数,但在折扣之后。根据税收设置,您可能需要添加税收。事情是这样的:

$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();
于 2020-05-12T08:58:35.943 回答
0

$totalamount = $woocommerce->cart->cart_contents_total;

回声$总金额;

于 2018-08-16T12:19:13.427 回答