php Woocommerce折扣:不含税添加负费用

irlmq6kh  于 2023-11-16  发布在  PHP
关注(0)|答案(3)|浏览(110)

我添加一个WooCommerce购物车费用如下:

add_action('woocommerce_cart_calculate_fees', function($cart) { 
      $cart->add_fee(__('Popust za vjernost'), -10, 0);
  });

字符串
我以为添加一个0作为第三个参数会将税设置为0,但WooCommerce仍然会将税添加到费用中。
如何将税设置为0?

bd1hkmkf

bd1hkmkf1#

覆盖核心文件不是一个解决方案,因为它会影响其他进程,并且每次更新WooCommerce时都会丢失更改。
使用WC_Cartadd_fee()方法与负金额,是一个调整添加折扣.在这种情况下,“应税”可选第三个参数不起作用,和税收总是适用.这已经报告给WooCommerce很多次,回应是WC_Cartadd_fee()方法不是为折扣.

1)关于WC_Cartadd_fee()方法4个可用参数的提醒:

/**
 * Add additional fee to the cart.
 *
 * This method should be called on a callback attached to the
 * woocommerce_cart_calculate_fees action during cart/checkout. Fees do not
 * persist.
 *
 * @uses WC_Cart_Fees::add_fee
 * @param string $name      Unique name for the fee. Multiple fees of the same name cannot be added.
 * @param float  $amount    Fee amount (do not enter negative amounts).
 * @param bool   $taxable   Is the fee taxable? (default: false).
 * @param string $tax_class The tax class for the fee if taxable. A blank string is standard tax class. (default: '').
 */
WC()->cart->add_fee( $name, $amount, $taxable, $tax_class );

字符串

2)使“taxable”第三个参数可以处理负数金额:

以下代码将在使用负金额时启用“taxable”,**当 *“taxable”参数设置为false**时,允许添加不含税的折扣

// Adjust cart total amount
add_filter( 'woocommerce_cart_get_total', 'filter_cart_get_total', 10, 1 );
function filter_cart_get_total( $total ) {
    $tax_amount = 0;

    foreach( WC()->cart->get_fees() as $fee ) {
        if( ! $fee->taxable && $fee->tax < 0 ) {
            $tax_amount -= $fee->tax;
        }
    }

    if( $tax_amount != 0 ) {
        $total += $tax_amount;
    }
    return $total;
}

// Adjust Fee taxes (array of tax totals)
add_filter( 'woocommerce_cart_get_fee_taxes', 'filter_cart_get_fee_taxes', 10, 1 );
function filter_cart_get_fee_taxes( $fee_taxes ) {
    $fee_taxes = array();
    
    foreach( WC()->cart->get_fees() as $fee ) {
        if( $fee->taxable ) {
            foreach( $fee->tax_data as $tax_key => $tax_amount ) {
                if( isset($fee_taxes[$tax_key]) ) {
                    $fee_taxes[$tax_key] += $tax_amount;
                } else {
                    $fee_taxes[$tax_key] = $tax_amount;
                }
            }
        }
    }
    return $fee_taxes;
}

// Displayed fees: Remove taxes from non taxable fees with negative amount
add_filter( 'woocommerce_cart_totals_fee_html', 'filter_cart_totals_fee_html', 10, 2 );
function filter_cart_totals_fee_html( $fee_html, $fee ) {
    if( ! $fee->taxable && $fee->tax < 0 ) {
        return wc_price( $fee->total );
    }
    return $fee_html;
}

// Adjust Order fee item(s) for negative non taxable fees
add_action( 'woocommerce_checkout_create_order_fee_item', 'alter_checkout_create_order_fee_item', 10, 4 );
function alter_checkout_create_order_fee_item( $item, $fee_key, $fee, $order ) {
    if ( ! $fee->taxable && $fee->tax < 0 ) {
        $item->set_taxes(['total' => []]);
        $item->set_total_tax(0);
    }
}


代码放在子主题的functions.php文件中(或插件中)。

3)使用Add_fee()进行测试:

下面,我们使用负金额添加不含税的折扣:

add_action( 'woocommerce_cart_calculate_fees', 'add_discount_without_tax' );
function add_discount_without_tax( $cart ) { 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    
    // Add a NON taxable DISCOUNT (negative amount, "taxable" argument set to "false")
    $cart->add_fee(__('Discount (no tax)'), -10, false); 
};


这一次它的工作原理,税收不适用,当订单提交,同样的事情。
代码放在子主题的functions.php文件中(或插件中)。

ryhaxcpt

ryhaxcpt2#

要将WooCommerce购物车费用的税设置为0,您可以使用add_fee方法的第四个参数来指定不应应用税。以下是如何修改代码:

add_action('woocommerce_cart_calculate_fees', function($cart) { 
    $cart->add_fee(__('Popust za vjernost'), -10, false, '');
});

字符串
通过传递false作为第三个参数,并传递一个空字符串''作为第四个参数,您表示不应对费用征税。这应该可以防止WooCommerce向费用征税。

ki0zmccv

ki0zmccv3#

我建议比LoicTheAztec提供的更短,更容易理解的解决方案。感谢他的灵感。您可以通过更改'total' => array()中的值来修改税费

private function cart_apply_negative_fee( $fee_name, $amount ) {
    add_action(
        'woocommerce_cart_calculate_fees',
        function ( $cart ) use ( $fee_name, $amount ) {
            $cart->add_fee( $fee_name, $amount * -1 );
        }
    );
    $this->cart_override_fee_tax_to_zero( $fee_name );
    $this->order_override_fee_tax_to_zero( $fee_name );
    WC()->cart->calculate_totals();
}

private function cart_override_fee_tax_to_zero( $fee_name ) {
    add_filter(
        'woocommerce_cart_totals_get_fees_from_cart_taxes',
        function ( $fee_taxes, $fee, $cart ) use ( $fee_name ) {
            if ( $fee->object->name !== $fee_name ) {
                return $fee_taxes;
            }
            return array( 'total' => 0 );
        },
        10,
        3
    );
}

private function order_override_fee_tax_to_zero( $fee_name ) {
    add_action(
        'woocommerce_order_item_fee_after_calculate_taxes',
        function ( $order_item_fee, $calculate_tax_for ) use ( $fee_name ) {
            if ( $order_item_fee->get_name() !== $fee_name ) {
                return;
            }
            $order_item_fee->set_taxes( array( 'total' => array() ) );
        },
        10,
        2
    );
}

字符串

相关问题