根据 Woocommerce 中的购物车商品价格有条件地设置不同的税率 [英] Set different Tax rates conditionally based on cart item prices in Woocommerce
问题描述
在 My Wordpress 电子商务网站中,我使用 WP Hotel Booking,这是一个用于酒店房间预订的插件.结帐过程是使用 WooCommerce 完成的.
问题:我们有不同价格的不同房间.例如:
- A 房价格 - 1500
- B 房价格 - 2700
- C 房价格 - 2200
价格低于 2500 的客房的商品及服务税设定为 12%,高于 2500 的客房的商品及服务税设定为 18%.
由于我使用 WP Hotel Booking 用于此自定义产品(房间管理),因此我无法使用 woocommerce 中的 Additional Tax Classes 选项来设置不同的税种.
我需要你帮忙编写一个函数来检查房间价值,然后决定需要为给定房间设置什么税.
谢谢
这很容易上手.
1°) 您需要在 WooCommerce 税收设置中创建 2 个新的税收类别.在此示例中,我将这些税种命名为Tax 12
"和Tax 18
".然后,您必须为每一个设置不同的 12%
和 18%
百分比.>
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 <2500 )$cart_item['data']->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 - 1500
- Room B Price - 2700
- Room c price - 2200
GST 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 中的购物车商品价格有条件地设置不同的税率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!