問題描述
我嘗試根據我在 Woocommerce 購物車上所做的一些計算來增加費用,但我想將其從增值稅中排除.這是我的代碼:
function woo_add_cart_fee( $cart ) {全球 $woocommerce;$bookable_total = 0;foreach(WC()->cart->get_cart() as $cart_item_key => $values) {$_product = $values['data'];//做我的事情來計算 $fee 變量WC()->cart->add_fee('費用:', $fee, false, '');//WC()->cart->add_fee('費用:', $fee, true, '' );//WC()->cart->add_fee('費用:', $fee, false, '零利率' );//WC()->cart->add_fee('費用:', $fee, true, '零利率' );}add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee');
我已經嘗試了所有評論版本,每個版本都包含增值稅.
知道如何實現它嗎?
(更新):TAX OPTIONS with
<塊引用>類 WC_Cart add_fee() 方法,向購物車添加額外費用.
add_fee( string $name, float $amount, boolean $taxable = false, string $tax_class = '' )
參數:$name 費用的唯一名稱.不能疊加多個同名費用.$amount 費用金額.$taxable(默認值:false)費用是否應納稅?$tax_class (default: '') 費用的稅類(如果應稅的話).空白字符串是標準稅種.
<小時>
原始答案 (更新代碼):
<塊引用>您的主要問題在于這一行:global $woocommerce, $bookable_total = 0;
- 因為您使用的是
WC()->cart
語法而不是$woocommerce->cart
> 語法,你真的不需要global $woocommerce;
. - 如果您使用
global $bookable_total = 0;
這個$bookable_total
將總是= 0
.
相反,您將使用global $bookable_total;
沒有值 來獲取在您的函數之外定義的值.
但是,如果您想將值設置為零,以防未在您的函數之外定義,您可以這樣做:woo_add_cart_fee( $bookable_total=0 )
我們現在可以在函數外定義$bookable_total
變量值.
這是您的代碼的工作示例:
//這個變量值傳遞給我們的函數$bookable_total = 1;函數woo_add_cart_fee($bookable_total = 0){全球 $bookable_total;if ( is_admin() && !defined( 'DOING_AJAX' ) )返回;//僅針對此示例$item_count = 0;$item_fee = 5;//遍歷每個購物車項目foreach(WC()->cart->get_cart() 作為 $values ) {$item = $values['data'];如果(空($item))休息;//獲取購物車 item_id$item_id = $item->id;$item_count++;//你的計算}//我們測試 $bookable_total 值,在我們的函數外定義為1"http://如果未在外部定義,則為 'O'(在這種情況下,費用將為 '0')$fee = $item_count * $bookable_total * $item_fee;//add_fee 方法(此處不應用 TAX)WC()->cart->add_fee('費用:', $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 = '' )
方法
I try to add fees based on some calculations I do on Woocommerce cart, but I want to exclude it from VAT. This is my code:
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' );
I have tried all the commented versions and each of them includes a VAT on Fees too.
Any idea how I can achieve it?
(Update): TAX OPTIONS with add_fee() method
IMPORTANT: The fact that TAX is working or not with
add_fee()
method depends first of your tax settings in woocommerce. As you have not tell in your question what are your TAX settings, It's not possible to help you (Tax settings can be much more different for each e-commerce web site).
For example if you want to use 'zero rate' tax class, but you haven't defined the correct 'zero rate' tax class for the customer country, this will not work if you try to use it with:
WC()->cart->add_fee( 'Fees: ', $fee, true, 'zero rate' );
… depending on your global tax settings.
Here is a screenshot of REAL checkout totals, for 3 items in cart (using the code below):
Class WC_Cart add_fee() method, adds additional fee to the cart.
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.
Original answer (updated code):
Your main problem is in this line:
global $woocommerce, $bookable_total = 0;
- As you are using
WC()->cart
syntax instead of$woocommerce->cart
syntax, you don't really needglobal $woocommerce;
.- If you use
global $bookable_total = 0;
this$bookable_total
will be always= 0
.
Instead you will useglobal $bookable_total;
without a value to get the value defined outside your function.
But if you want to set the value to zero, in case is not defined outside your function, you will do it this way:woo_add_cart_fee( $bookable_total=0 )
We can defined now $bookable_total
variable value outside the function.
This is a working example with your code:
// 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' );
This code is tested and it works. It goes on function.php file of your active child theme or theme.
If the $bookable_total
variable is not defined outside, the value will be 0
.
Note: is better to get the $items ids with:
$item = $values['data']; $item_id = $item->id;
Reference:
Class WC_Cart - add_fee( $name, $amount, $taxable = false, $tax_class = '' )
method
這篇關于以編程方式向 WooCommerce 購物車添加免稅費用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!