简体   繁体   English

WooCommerce四舍五入后计算总计

[英]WooCommerce rounding before calculate totals

I'm using WooCommerce version 3.0+ as well as a price by user roles plugin which gives a % discount against prices. 我使用的是WooCommerce 3.0+版本,以及按用户角色提供的价格插件,该插件提供了针对价格的%折扣。

We have an item in the shop which is £89.55. 我们商店里有一件东西是£89.55。 With a 30% discount applied this becomes £62.685. 加上30%的折扣,则变成£62.685。 If you order 6 then the value is £376.11. 如果您订购6,则价值为376.11英镑。

The final figure is correct. 最终数字是正确的。

However as I have WC settings set to 2dp, the item price shows as £62.69. 但是,由于我将WC设置设置为2dp,因此商品价格显示为62.69英镑。 Therefore invoices are incorrect showing as £62.69 x 6 = £376.11. 因此,发票不正确,显示为£62.69 x 6 =£376.11。

I've thought about using woocommerce_before_calculate_totals like from here: 我已经考虑过从这里使用woocommerce_before_calculate_totals:

Dynamic cart item pricing not working on orders in WooCommerce 3.0+ 动态购物车项目定价不适用于WooCommerce 3.0+中的订单

My code is: 我的代码是:

add_action( 'woocommerce_before_calculate_totals', 'adding_custom_price', 10, 1);
function adding_custom_price( $cart_obj ) {

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

 foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
     $new_price = round($cart_item['data']->price,2,PHP_ROUND_HALF_UP);
     $cart_item['data']->set_price($new_price);
     #echo $new_price;
 }

} }

The output from the echo $new_price is 62.69. 回声$ new_price的输出为62.69。 But it seems set_price isn't working as the values in the cart are still showing as for 62.685. 但是似乎set_price无法正常工作,因为购物车中的值仍显示为62.685。

So for example if I do 62.69 x 2 the subtotal is 125.37. 因此,例如,如果我执行62.69 x 2,则小计为125.37。

Any idea why set_price isn't working? 知道为什么set_price不起作用吗? I saw this: 我看见了这个:

Woocommerce: $cart_item['data']->set_price is not working inside custom plugin Woocommerce:$ cart_item ['data']-> set_price在自定义插件中不起作用

But the answer on there doesn't work either. 但是那里的答案也不起作用。

Any help much appreciated. 任何帮助,不胜感激。

First you need to use WC_Product method get_price() as $cart_item['data'] is an instance of WC_Simple_Product . 首先,您需要使用WC_Product方法get_price()因为$cart_item['data']WC_Simple_Product的实例。

Also you have to beware as the displayed prices in cart are already rounded by WooCommerce specific formatting functions. 另外,您还必须提防,因为购物车中的显示价格已经通过WooCommerce特定格式功能进行了四舍五入。

So your functional code for WC 3.0+ will be: 因此,您的WC 3.0+的功能代码将是:

add_action( 'woocommerce_before_calculate_totals', 'adding_custom_price', 10, 1);
function adding_custom_price( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach ( $cart->get_cart() as $cart_item ) {
        $product_price = $cart_item['data']->get_price(); // get the price
        $rounded_price = round( $product_price, 2, PHP_ROUND_HALF_UP );
        $cart_item['data']->set_price(floatval($rounded_price));
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。


You can check (test) by adding to the product price (cart item) a very little float number. 您可以通过在产品价格(购物车产品)中添加一个很少的浮点数来进行检查(测试)。 You will see that the cart prices are really updated, replacing for example in the function: 您将看到购物车价格确实已更新,例如替换了以下功能:

$cart_item['data']->set_price( floatval( $rounded_price ) );

By 通过

$cart_item['data']->set_price( floatval( $rounded_price + 0.2 ) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM