问题描述
我遇到购物车总数的问题只显示 0
I am running into issues with the cart total only displaying 0
基本上,我想要做的只是在将所有购物车项目添加到购物车小计后才接受一定数量的存款总额.
Essentially what I am trying to do is only accept a deposit total of a certain amount after all cart items have been added to the carts subtotal.
例如,如果客户添加价值 100 美元的商品,他们最初只需支付 10 美元或小计的 (10%) 作为总价值.
So for example if the customer adds $100 worth of items, they would only pay $10 initially or (10%) of the subtotal as the total value.
我从这里获取了代码:更改 total 和 tax_total Woocommerce 和以这种方式自定义:
I took the code from here: Change total and tax_total Woocommerce and customize it this way:
add_action('woocommerce_cart_total', 'calculate_totals', 10, 1);
function calculate_totals($wc_price){
$new_total = ($wc_price*0.10);
return wc_price($new_total);
}
但启用该代码时,总金额显示为 0.00.如果删除代码,我会得到标准总数.
But the total amount shows 0.00 when that code is enabled. If removed the code, I get the standard total.
我也无法在 woocommerce 网站上找到列出完整 api 的内容,只有与如何创建插件相关的通用文章.
I also could not find on the woocommerce site where the full api is listed, only generic articles related to how to create a plugin.
任何帮助或正确方向的点都会很棒.
Any help or a point in the right direction would be great.
推荐答案
新答案:更改在 Woocommerce 3.2+ 中使用 Hooks 的购物车总数
第一个 woocommerce_cart_total
钩子是一个 filter 钩子,而不是一个动作钩子.此外,由于 woocommerce_cart_total
中的 wc_price
参数是格式化价格,您将不会可以增加 10%.这就是它返回零的原因.
First woocommerce_cart_total
hook is a filter hook, not an action hook. Also as wc_price
argument in woocommerce_cart_total
is the formatted price, you will not be able to increase it by 10%. That's why it returns zero.
在 Woocommerce v3.2 之前,它像一些 WC_Cart
属性可以直接访问
您应该更好地使用在 woocommerce_calculate_totals
动作挂钩中挂钩的自定义函数
这样:
You should better use a custom function hooked in woocommerce_calculate_totals
action hook
this way:
// Tested and works for WooCommerce versions 2.6.x, 3.0.x and 3.1.x
add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );
function action_cart_calculate_totals( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( !WC()->cart->is_empty() ):
## Displayed subtotal (+10%)
// $cart_object->subtotal *= 1.1;
## Displayed TOTAL (+10%)
// $cart_object->total *= 1.1;
## Displayed TOTAL CART CONTENT (+10%)
$cart_object->cart_contents_total *= 1.1;
endif;
}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
也可以使用WC_cartadd_fee()
这个钩子中的方法,或者像 Cristina 答案一样单独使用它.
这篇关于在 WooCommerce 中更改购物车总价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!