您如何functions.php
在 WordPress 的页面中获取 WooCommerce 中的税款总额,使用:
global $woocommerce;
$discount = $woocommerce->cart->tax_total;
但是没有返回任何值。
如何获得购物车税总额?
本质上,我希望为用户计算税款,但随后将其减少,因为客户将支付 COD 税款。
完整代码如下:
add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );
function action_cart_calculate_totals( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( !WC()->cart->is_empty() ):
$cart_object->cart_contents_total *= .10 ;
endif;
}
//Code for removing tax from total collected
function prefix_add_discount_line( $cart ) {
global $woocommerce;
$discount = $woocommerce->cart->tax_total;
$woocommerce->cart->add_fee( __( 'Tax Paid On COD', 'your-text-domain' ) , - $discount );
}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' );