php 在 WooCommerce 中更改购物车总价

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43415783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:49:30  来源:igfitidea点击:

Change Cart total price in WooCommerce

phpwordpresswoocommercecarthook-woocommerce

提问by DEVPROCB

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 Woocommerceand 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.00when that code is enabled. If removed the code, I get the standard total.

但是当启用该代码时,总金额显示为 0.00。如果删除代码,我得到标准总数。

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.

任何帮助或正确方向的观点都会很棒。

回答by Christina

This does not answer this question. Loic's does. This is another way of doing it to show a line item of 10% off:

这并不能回答这个问题。Loic的。这是显示 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' );

enter image description here

在此处输入图片说明

回答by LoicTheAztec

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

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

由于 Woocommerce 3.2+ 它不再适用于新的 Class WC_Cart_Totals...

新答案:在 Woocommerce 3.2+ 中使用 Hooks 更改购物车总数



First woocommerce_cart_totalhook is a filterhook, not an action hook. Also as wc_priceargument in woocommerce_cart_totalis the formatted price, you will not be able to increase it by 10%. That's why it returns zero.

第一个woocommerce_cart_total钩子是一个过滤器钩子,而不是一个动作钩子。此外,由于wc_price参数woocommerce_cart_total格式化的 price,您将无法将其增加 10%。这就是它返回零的原因。

Before Woocommerce v3.2it works as some WC_Cartpropertiescan be accessed directly

在 Woocommerce v3.2 之前,可以直接访问某些WC_Cart属性

You should better use a custom function hooked in woocommerce_calculate_totalsaction hook
this way:

您最好以这种方式使用挂钩在woocommerce_calculate_totals动作挂钩中的自定义函数

// 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_cartadd_fee()method in this hook, or use it separately like in Cristinaanswer.

也可以在这个钩子中使用WC_cartadd_fee()方法,或者像Cristina 的回答一样单独使用它。