在WooCommerce中更改购物车总价 [英] Change Cart total price in WooCommerce

查看:250
本文介绍了在WooCommerce中更改购物车总价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到购物车总额问题,仅显示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.

我在列出完整api的woocommerce网站上也找不到,只有与如何创建插件有关的通用文章.

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+起 它不再适用于新类 ...

Since Woocommerce 3.2+ it does not work anymore with the new Class WC_Cart_Totals ...

新答案:更改在Woocommerce 3.2+

New answer: Change Cart total using Hooks in Woocommerce 3.2+


第一个 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属性可以直接访问

Before Woocommerce v3.2 it works as some WC_Cart properties can be accessed directly

您最好使用通过以下方式钩在 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_cart add_fee() 方法,或像 Cristina 答案中那样单独使用它.

Is also possible to use WC_cart add_fee() method in this hook, or use it separately like in Cristina answer.

这篇关于在WooCommerce中更改购物车总价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆