在 My Wordpress 电子商务网站中,我使用WP Hotel Booking,这是一个用于酒店房间预订的插件。结帐过程是使用WooCommerce完成的。
问题:我们有不同价格的不同房间。例如:
价格低于 2500 的房间的商品及服务税设定为 12%,高于 2500 的房间为 18%。
由于我为此自定义产品(房间管理)使用WP Hotel Booking,因此我无法使用woocommerce 中的附加税种选项来设置不同的税种。
我需要你帮忙写一个函数来检查房间价值,然后决定需要为给定房间设置什么税。
谢谢
这是一件容易上手的事情。
1°) 您需要在 WooCommerce 税设置中创建 2 个新税类。在本例中,我将这些税种命名为“ Tax 12
”和“ Tax 18
”。然后,对于它们中的每一个,您都必须设置不同的12%
和百分比18%
。
2°) 现在这里有一个挂在woocommerce_before_calculate_totals
action hook 中的自定义函数,它将根据产品价格应用税类。我不使用税类名称,而是使用小写和空格的税类 slugs由连字符替换。
所以这是代码:
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
}
}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。
此代码经过测试并适用于 WooCommerce 版本 3+
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句