微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 在WooCommerce中以编程方式获取购物车税

如何在wordpress的functions.PHP页面获取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' );

解决方法:

>全球$woocommerce; $woocommerce->购物车已淘汰.请改用WC() – >购物车.
在这里你可以直接使用$cart(object)参数代替……
>正确的属性是tax而不是tax_total.
>最好使用WC_Cart get_taxes()方法intead与WooCommerce 3.0版兼容

实现您的目标您的代码将是:

// For Woocommerce 2.5+ (2.6.x and 3.0)
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line', 10, 1 );
function prefix_add_discount_line( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $discount = 0;
    // Get the unformated taxes array
    $taxes = $cart->get_taxes(); 
    // Add each taxes to $discount
    foreach($taxes as $tax) $discount += $tax;

    // Applying a discount if not null or equal to zero
    if ($discount > 0 && ! empty($discount) )
        $cart->add_fee( __( 'Tax Paid On COD', 'your-text-domain' ) , - $discount );
}

代码放在活动子主题(或主题)的function.PHP文件中,或者放在任何插件文件中.

代码经过测试和运行.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐