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

编程入门 行业动态 更新时间:2024-10-12 03:16:23
本文介绍了以编程方式将免税费添加到WooCommerce购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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?

推荐答案

(更新):带有 add_fee()方法

(Update): TAX OPTIONS with add_fee() method

重要提示:使用 add_fee()方法 取决于woocommerce中的您的第一笔税收设置.由于您的问题不知道,您的税金设置是什么,因此无法帮助您(每个电子商务的税金设置可能会大不相同网站).

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).

例如,如果您要使用零税率"税种,但您尚未为客户所在国家/地区定义正确的零税率"税种,则尝试使用此方法将不起作用配合使用: WC()->cart->add_fee( 'Fees: ', $fee, true, 'zero rate' ); …取决于您的全局税设置.

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.

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

Here is a screenshot of REAL checkout totals, for 3 items in cart (using the code below):

类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;

Your main problem is in this line: 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 )
  • As you are using WC()->cart syntax instead of $woocommerce->cart syntax, you don't really need global $woocommerce;.
  • If you use global $bookable_total = 0; this $bookable_total will be always = 0. Instead you will use global $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 )
  • 我们现在可以在函数外定义 $bookable_total变量值.

    We can defined now $bookable_total variable value outside the function.

    这是您的代码的有效示例:

    // 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 .

    If the $bookable_total variable is not defined outside, the value will be 0.

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

    Note: is better to get the $items ids with: $item = $values['data']; $item_id = $item->id;

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

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

    更多推荐

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

    本文发布于:2023-11-24 21:51:51,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1626988.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:税费   购物车   方式   WooCommerce

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!