Php 商业中特定国家的简单加权应税费用
在Woocommerce中,我使用以下代码块,根据特定国家/地区的总重量在购物车和结帐中添加自定义费用:Php 商业中特定国家的简单加权应税费用,php,wordpress,woocommerce,cart,fee,Php,Wordpress,Woocommerce,Cart,Fee,在Woocommerce中,我使用以下代码块,根据特定国家/地区的总重量在购物车和结帐中添加自定义费用: function weight_add_cart_fee() { // Set here your percentage $percentage = 0.17; if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Get weight of all items
function weight_add_cart_fee() {
// Set here your percentage
$percentage = 0.17;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Get weight of all items in the cart
$cart_weight = WC()->cart->get_cart_contents_weight();
// calculate the fee amount
$fee = $cart_weight * $percentage;
// If weight amount is not null, adds the fee calcualtion to cart
global $woocommerce;
$country = $woocommerce->customer->get_country();
if ( !empty( $cart_weight ) && $country == 'SK' ) {
WC()->cart->add_fee( __('Recyklačný poplatok (podľa váhy): ', 'my_theme_slug'), $fee, false );
}
}
add_action( 'woocommerce_cart_calculate_fees','weight_add_cart_fee' );
但我需要让这笔费用应税。如何征税?有一些错误,您的代码过时了。请尝试以下方法(收取应税的费用): 代码进入活动子主题(或活动主题)的function.php文件。测试和工作 使费用应税 要使费用应税,在中,需要将第3个参数设置为true(应税) 第四个可选参数与
税类相关,如果需要设置特定税类,可以指定该税类
add_action( 'woocommerce_cart_calculate_fees','add_fee_weight_based', 10 , 1 );
function add_fee_weight_based( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.17; // Percentage
$targeted_country = 'SK'; // Country
$cart_weight = $cart->get_cart_contents_weight(); // Total weight
if ( $cart_weight > 0 && WC()->customer->get_shipping_country() == $targeted_country ) {
$cart->add_fee( __('Recyklačný poplatok (podľa váhy): ', 'my_theme_slug'), ($cart_weight * $percentage), true );
}
}