2°) 现在这是一个挂在 woocommerce_before_calculate_totals 动作钩子中的自定义函数,它将根据产品价格应用税级.我不使用税类名称,而是使用税类标头,它们是小写的,空格由连字符替换.代码如下:add_action( 'woocommerce_before_calculate_totals', 'change_cart_items_prices', 10, 1 );函数 change_cart_items_prices( $cart ) {if ( is_admin() && !defined( 'DOING_AJAX' ) )返回;if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )返回;foreach ( $cart->get_cart() as $cart_item ) {//获取商品价格$price = $cart_item['data']->get_price();//根据税种的价格有条件地设置如果 ( $price set_tax_class('tax-12');//低于 2500如果 ( $price >= 2500 )$cart_item['data']->set_tax_class('tax-18');//高于 2500}}代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.此代码经过测试,可在 WooCommerce 版本 3+ 上运行In My Wordpress e-commerce web site I use WP Hotel Booking, a plugin for hotel room bookings. The checkout process is done using WooCommerce.The Issue: We have different rooms with different pricing.For example :Room A price - 1500Room B Price - 2700Room c price - 2200GST Tax is set at 12% for rooms wich price is below 2500 and 18% for rooms above 2500.Since I am using WP Hotel Booking for this custom product (room Management), I am unable to use the Additional Tax Classes option in woocommerce to set different tax classes.I need your help in writing a function to check the room value and then decide what tax needs to be set for the given room.Thanks 解决方案 This is something accessible and easy.1°) you need to create in your WooCommerce Tax settings 2 new Tax classes. In this example I have named that tax classes "Tax 12" and "Tax 18". Then for each of them you will have to set a different percentage of 12% and 18%.2°) Now here is a custom function hooked in woocommerce_before_calculate_totals action hook that is going to apply a tax class based on the product price. I don't use the tax class names, but the tax class slugs, that are in lowercase and spaces are replace by a hyphen.So Here is that code:add_action( 'woocommerce_before_calculate_totals', 'change_cart_items_prices', 10, 1 );function change_cart_items_prices( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return; foreach ( $cart->get_cart() as $cart_item ) { // get product price $price = $cart_item['data']->get_price(); // Set conditionaly based on price the tax class if ( $price < 2500 ) $cart_item['data']->set_tax_class( 'tax-12' ); // below 2500 if ( $price >= 2500 ) $cart_item['data']->set_tax_class( 'tax-18' ); // Above 2500 }}Code goes in function.php file of your active child theme (or theme) or also in any plugin file.This code is tested and works on WooCommerce version 3+ 这篇关于根据 Woocommerce 中的购物车商品价格有条件地设置不同的税率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-13 23:04