[英]Change Cart total price in WooCommerce
I am running into issues with the cart total only displaying 0我遇到了购物车总数仅显示 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.本质上,我想要做的只是在将所有购物车项目添加到购物车小计后才接受一定数量的存款总额。
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.因此,例如,如果客户添加价值 100 美元的商品,他们最初只需支付 10 美元或小计的 (10%) 作为总价值。
I took the code from here: Change total and tax_total Woocommerce and customize it this way:我从这里获取了代码: Change total 和 tax_total Woocommerce并以这种方式对其进行自定义:
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);
}
But the total amount shows 0.00 when that code is enabled.但是当启用该代码时,总金额显示为 0.00 。 If removed the code, I get the standard total.如果删除代码,我得到标准总数。
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.我也无法在 woocommerce 网站上找到列出完整 api 的内容,只有与如何创建插件相关的通用文章。
Any help or a point in the right direction would be great.任何帮助或正确方向的观点都会很棒。
This does not answer this question.这并不能回答这个问题。 Loic's does. Loic的。 This is another way of doing it to show a line item of 10% off:这是显示 10% 折扣的订单项的另一种方式:
function prefix_add_discount_line( $cart ) {
$discount = $cart->subtotal * 0.1;
$cart->add_fee( __( 'Down Payment', 'yourtext-domain' ) , -$discount );
}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' );
Since Woocommerce 3.2+ it does not work anymore with the new Class
WC_Cart_Totals
...从 Woocommerce 3.2+ 开始,它不再适用于新的 ClassWC_Cart_Totals
...New answer: Change Cart total using Hooks in Woocommerce 3.2+新答案: 在 Woocommerce 3.2+ 中使用 Hooks 更改购物车总数
First woocommerce_cart_total
hook is a filter hook, not an action hook.首先woocommerce_cart_total
钩子是一个过滤器钩子,而不是一个动作钩子。 Also as wc_price
argument in woocommerce_cart_total
is the formatted price , you will not be able to increase it by 10%.另外,作为wc_price
在参数woocommerce_cart_total
是格式化的价格,您将无法通过增加10%吧。 That's why it returns zero.这就是它返回零的原因。
Before Woocommerce v3.2 it works as some
WC_Cart
properties can be accessed directly在 Woocommerce v3.2 之前,它可以直接访问某些WC_Cart
属性
You should better use a custom function hooked in woocommerce_calculate_totals
action hook您应该更好地使用挂钩在woocommerce_calculate_totals
动作挂钩中的自定义函数
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;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。
Is also possible to use WC_cart
add_fee()
method in this hook, or use it separately like in Cristina answer.也可以在这个钩子中使用WC_cartadd_fee()
方法,或者像Cristina 的回答一样单独使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.