本文介绍了根据Woocommerce中的购物车价格有条件地设置不同的税率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Wordpress电子商务网站上,我使用 WP酒店预订,这是用于酒店房间预订的插件.结帐过程是使用 WooCommerce 完成的.

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 :

  • A房间价格-1500
  • B房间价格-2700
  • c室价格-2200

对于价格在2500以下的客房,将其商品及服务税的税率定为12%,对于价格在2500以上的客房,其商品及服务税的税率为18%.

GST Tax is set at 12% for rooms wich price is below 2500 and 18% for rooms above 2500.

由于我正在为此定制产品(客房管理)使用 WP酒店预订,因此我无法在woocommerce中使用其他税种选项来设置不同的税种

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.

谢谢

推荐答案

这是易于访问和方便的事情.

This is something accessible and easy.

1°),您需要在WooCommerce税设置中创建2个新的税类.在此示例中,我将税种命名为" Tax 12 "和" Tax 18 ".然后,必须为它们中的每一个设置不同百分比的 12% 18% .

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°)现在,这是一个挂钩在 woocommerce_before_calculate_totals 动作挂钩中的自定义函数,它将根据产品价格应用税种.我没有使用税种名称,但是税种标段是小写的,空格用连字符代替.

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.

所以这是代码:

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文件中,也可能会出现在任何插件文件中.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

此代码已经过测试,可在WooCommerce版本3+上运行

This code is tested and works on WooCommerce version 3+

这篇关于根据Woocommerce中的购物车价格有条件地设置不同的税率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 05:24