WooCommerce 在计算总数之前四舍五入
WooCommerce rounding before calculate totals
我使用的是 WooCommerce 3.0+ 版以及按用户角色分类的价格插件,它提供了 % 的价格折扣。
我们店里有一件 89.55 英镑的商品。应用 30% 的折扣后,这将变为 62.685 英镑。如果您订购 6 件,则价值为 £376.11。
最终数字正确。
但是,由于我将 WC 设置设置为 2dp,商品价格显示为 62.69 英镑。因此,发票显示为 £62.69 x 6 = £376.11 是不正确的。
我考虑过使用 woocommerce_before_calculate_totals 就像从这里:
我的代码是:
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;
}
}
echo $new_price 的输出是 62.69。但似乎 set_price 不起作用,因为购物车中的值仍显示为 62.685。
例如,如果我计算 62.69 x 2,则小计为 125.37。
知道为什么 set_price 不起作用吗?我看到了这个:
Woocommerce: $cart_item['data']->set_price is not working inside custom plugin
但是那里的答案也不行。
非常感谢任何帮助。
首先你需要使用WC_Product
方法get_price()
因为$cart_item['data']
是[=16=的一个实例]。
您还必须注意,购物车中显示的价格已经由 WooCommerce 特定的格式设置函数四舍五入。
因此您的 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));
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
您可以通过在产品价格 (购物车商品) 中添加一个非常小的浮点数来检查(测试)。您会看到购物车价格确实更新了,例如在函数中替换:
$cart_item['data']->set_price( floatval( $rounded_price ) );
由
$cart_item['data']->set_price( floatval( $rounded_price + 0.2 ) );
我使用的是 WooCommerce 3.0+ 版以及按用户角色分类的价格插件,它提供了 % 的价格折扣。
我们店里有一件 89.55 英镑的商品。应用 30% 的折扣后,这将变为 62.685 英镑。如果您订购 6 件,则价值为 £376.11。
最终数字正确。
但是,由于我将 WC 设置设置为 2dp,商品价格显示为 62.69 英镑。因此,发票显示为 £62.69 x 6 = £376.11 是不正确的。
我考虑过使用 woocommerce_before_calculate_totals 就像从这里:
我的代码是:
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;
}
}
echo $new_price 的输出是 62.69。但似乎 set_price 不起作用,因为购物车中的值仍显示为 62.685。
例如,如果我计算 62.69 x 2,则小计为 125.37。
知道为什么 set_price 不起作用吗?我看到了这个:
Woocommerce: $cart_item['data']->set_price is not working inside custom plugin
但是那里的答案也不行。
非常感谢任何帮助。
首先你需要使用WC_Product
方法get_price()
因为$cart_item['data']
是[=16=的一个实例]。
您还必须注意,购物车中显示的价格已经由 WooCommerce 特定的格式设置函数四舍五入。
因此您的 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));
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
您可以通过在产品价格 (购物车商品) 中添加一个非常小的浮点数来检查(测试)。您会看到购物车价格确实更新了,例如在函数中替换:
$cart_item['data']->set_price( floatval( $rounded_price ) );
由
$cart_item['data']->set_price( floatval( $rounded_price + 0.2 ) );