以编程方式向WooCommerce购物车免税费用

问题描述:

我尝试根据我在Woocommerce购物车上进行的一些计算添加费用,但我希望将其从增值税中排除。这是我的代码:以编程方式向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'); 

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

任何想法如何实现它?

+0

抱歉耽搁。我还联系了woo商业支持,他们告诉我,我的初始代码是正确的,并且可能与主题或其他插件有冲突。我将在星期一进行调查。 – Tasos

更新税选项与 add_fee() method

重要:税收正在或add_fee() method事实上取决于第一的woocommerce计税设置 。因为您有不能告诉在您的问题什么是您的税务设置,这是不可能的帮助您(税务设置可以有更多不同的每个电子商务网站)

例如,如果你想使用“零利率”税类,但是你有没有定义正确的“零利率”的税收,为客户全国一流,如果您尝试使用,这将不起作用它与:
WC()->cart->add_fee('Fees: ', $fee, true, 'zero rate'); ... 取决于您的全球税收设置

这里是REAL结帐总计的屏幕截图,用于在台车3项(用下面的代码):

enter image description here

类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. 如果使用global $bookable_total = 0;$bookable_total总是= 0
    取而代之,您将使用global $bookable_total;没有值以获取在您的函数外定义的值。
    但是,如果你想值设置为零,如果没有你的函数定义之外,你会做这样的: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

注:是更好地得到$项目IDS有:$item = $values['data']; $item_id = $item->id;


参考:
Class WC_Cart - add_fee($name, $amount, $taxable = false, $tax_class = '') method

+0

你好。谢谢你的详细解答,但我相信存在误解。 '$ bookable_total'只是一个我在循环中使用的变量,如果有特定的产品类型,它会改变。它不影响费用的数额。但是,要清楚。我的代码添加了费用,但它包含税。我想让这笔费用免税。这4个代码中哪些不是。 – Tasos

+0

@Tasos好的,我已经更新了我的答案。您的问题肯定与您的woocommerce网站有关**税务设置** ... – LoicTheAztec

+0

@Tasos Precision:在我截图的实例中,我使用(作为法国客户)20%的正常税率。所以如你所见税收不适用于费用。 – LoicTheAztec