以编程方式将免税费用添加到 WooCommerce 购物车

问题描述 投票:0回答:1

我尝试根据我在 Woocommerce 购物车上进行的一些计算添加费用,但我想将其从增值税中排除。这是我的代码:

function woo_add_cart_fee( $cart ) {
    global $woocommerce; $bookable_total = 0; 

    foreach(WC()->cart->get_cart() as $cart_item_key => $values) { 
        $_product = $values['data'];

        //doing my stuff to calculate $fee variable

    WC()->cart->add_fee( 'Fees: ', $fee, false, '' );
    //WC()->cart->add_fee( 'Fees: ', $fee, true, '' );
    //WC()->cart->add_fee( 'Fees: ', $fee, false, 'zero rate' );
    //WC()->cart->add_fee( 'Fees: ', $fee, true, 'zero rate' );
}

add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

我已经尝试了所有评论的版本,每个版本都包含增值税费用。

知道如何实现它吗?

php wordpress woocommerce cart fee
1个回答
4
投票

更新使用 add_fee() 方法的税务选项

重要提示: TAX 是否适用于 add_fee()

方法
取决于 woocommerce 中的第一个税收设置。由于您在问题中没有告诉您的税务设置是什么,因此不可能为您提供帮助(每个电子商务网站的税务设置可能会有很大不同)

特殊情况:使用add_fee()

方法
添加折扣(负数)时,始终适用税费

例如,如果您想使用

“零税率”税级,但尚未为客户所在国家/地区定义正确的“零税率”税级,则如果您尝试将其与以下方式一起使用,则此操作将不起作用:
WC()->cart->add_fee( 'Fees: ', $fee, true, 'zero rate' );

取决于您的全球税务设置

这是购物车中 3 件商品的真实结帐总额的屏幕截图(使用下面的代码):

类 WC_Cart add_fee() 方法,向购物车添加额外费用。

add_fee( string $name, float $amount, boolean $taxable = false, string $tax_class = '' )

Parameters:

$name Unique name for the fee. Multiple fees of the same name cannot be added. $amount Fee amount. $taxable (default: false) Is the fee taxable? $tax_class (default: '') The tax class for the fee if taxable. A blank string is standard tax class.


原始答案(更新代码)

您的主要问题在于这一行:global $woocommerce, $bookable_total = 0;


    由于您使用的是
  1. WC()->cart
     语法而不是 
    $woocommerce->cart
     语法,
    您实际上并不需要 global $woocommerce;
  2. 如果您使用
  3. global $bookable_total = 0;
    ,这个
    $bookable_total
    将是
    永远= 0
    相反,您将使用
    global $bookable_total;
     
    without a value 来获取函数外部定义的值。 但是如果你想
    将值设置为零,以防在你的函数之外没有定义,你可以这样做:woo_add_cart_fee( $bookable_total=0 )
我们现在可以在函数外部定义

$bookable_total

变量值。

这是您的代码的工作示例:

// This variable value is passed to our function $bookable_total = 1; function woo_add_cart_fee( $bookable_total = 0 ) { global $bookable_total; if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // just for this example $item_count = 0; $item_fee = 5; // going through each cart items foreach( WC()->cart->get_cart() as $values ) { $item = $values['data']; if ( empty( $item ) ) break; // getting the cart item_id $item_id = $item->id; $item_count++; // your calculations } // We test $bookable_total value, defined to '1' outside our function // and to 'O' if not defined outside (in this case the fee will be '0') $fee = $item_count * $bookable_total * $item_fee; // add_fee method (TAX will NOT be applied here) WC()->cart->add_fee( 'Fees: ', $fee, false ); } add_action( 'woocommerce_cart_calculate_fees','woo_add_cart_fee' );

此代码经过测试并且可以工作。

它位于您的活动子主题或主题的 function.php 文件中。 如果

$bookable_total变量没有在

外部定义,则该值将为
0

注意:

最好通过以下方式获取 $items id: $item = $values['data']; $item_id = $item->id;




参考:

类 WC_Cart -
add_fee( $name, $amount, $taxable = false, $tax_class = '' )

方法

© www.soinside.com 2019 - 2024. All rights reserved.