6

我尝试根据我在 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' );

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

知道如何实现吗?

4

1 回答 1

3

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

重要提示: TAX是否有效add_fee()事实首先取决于您在 woocommerce中的税收设置。由于您没有在问题中说明您的 TAX 设置是什么,因此无法为您提供帮助(每个电子商务网站的税收设置可能会大不相同)

例如,如果您想使用“零税率”税级,但您没有为客户国家/地区定义正确的“零税率”税级,如果您尝试将其用于以下情况,这将不起作用:
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. 如果你使用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.

注意:最好使用以下方式获取 $items id:$item = $values['data']; $item_id = $item->id;


参考:
WC_Cart 类 -add_fee( $name, $amount, $taxable = false, $tax_class = '' )方法

于 2016-08-11T17:12:29.063 回答