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

2023-01-16php开发问题
5

本文介绍了以编程方式向 WooCommerce 购物车添加免税费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

//这个变量值传递给我们的函数$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"//如果未在外部定义,则为 '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:

 解决方案 

(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 = "'  )

// 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 购物车添加免税费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17