pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

以編程方式向 WooCommerce 購物車添加免稅費用

Add tax free fees to WooCommerce cart programmatically(以編程方式向 WooCommerce 購物車添加免稅費用)
本文介紹了以編程方式向 WooCommerce 購物車添加免稅費用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我嘗試根據我在 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;

  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變量值.

這是您的代碼的工作示例:

//這個變量值傳遞給我們的函數$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;

  1. As you are using WC()->cart syntax instead of $woocommerce->cart syntax, you don't really need global $woocommerce;.
  2. 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 )

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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 雷达液位计_超声波风速风向仪_雨量传感器_辐射传感器-山东风途物联网 | 2025第九届世界无人机大会| 列管冷凝器,刮板蒸发器,外盘管反应釜厂家-无锡曼旺化工设备有限公司 | 新疆系统集成_新疆系统集成公司_系统集成项目-新疆利成科技 | 真空吸污车_高压清洗车厂家-程力专用汽车股份有限公司官网 | 电缆隧道在线监测-智慧配电站房-升压站在线监测-江苏久创电气科技有限公司 | 带压开孔_带压堵漏_带压封堵-菏泽金升管道工程有限公司 | 硬度计_影像测量仪_维氏硬度计_佛山市精测计量仪器设备有限公司厂家 | 斗式提升机,斗式提升机厂家-淄博宏建机械有限公司 | 济南网站建设|济南建网站|济南网站建设公司【济南腾飞网络】【荐】 | 济南电缆桥架|山东桥架-济南航丰实业有限公司 | 3d打印服务,3d打印汽车,三维扫描,硅胶复模,手板,快速模具,深圳市精速三维打印科技有限公司 | 粘度计NDJ-5S,粘度计NDJ-8S,越平水分测定仪-上海右一仪器有限公司 | 杭州成人高考_浙江省成人高考网上报名 | 氧化锆纤维_1800度高温退火炉_1800度高温烧结炉-南京理工宇龙新材料股份有限公司 | 二次元影像仪|二次元测量仪|拉力机|全自动影像测量仪厂家_苏州牧象仪器 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 伊卡洛斯软装首页-电动窗帘,别墅窗帘,定制窗帘,江浙沪1000+别墅窗帘案例 | 医用酒精_84消毒液_碘伏消毒液等医用消毒液-漓峰消毒官网 | 上海物流公司,上海货运公司,上海物流专线-优骐物流公司 | 青岛侦探_青岛侦探事务所_青岛劝退小三_青岛婚外情取证-青岛王军侦探事务所 | 沥青灌缝机_路面灌缝机_道路灌缝机_沥青灌缝机厂家_济宁萨奥机械有限公司 | 滚筒烘干机_转筒烘干机_滚筒干燥机_转筒干燥机_回转烘干机_回转干燥机-设备生产厂家 | 膜结构停车棚-自行车棚-膜结构汽车棚加工安装厂家幸福膜结构 | 宝元数控系统|对刀仪厂家|东莞机器人控制系统|东莞安川伺服-【鑫天驰智能科技】 | 管理会计网-PCMA初级管理会计,中级管理会计考试网站 | 河南档案架,档案密集架,手动密集架,河南密集架批发/报价 | 无锡不干胶标签,卷筒标签,无锡瑞彩包装材料有限公司 | 东莞工厂厂房装修_无尘车间施工_钢结构工程安装-广东集景建筑装饰设计工程有限公司 | 派克防爆伺服电机品牌|国产防爆伺服电机|高低温伺服电机|杭州摩森机电科技有限公司 | 混合气体腐蚀试验箱_盐雾/硫化氢/气体腐蚀试验箱厂家-北京中科博达 | 大_小鼠elisa试剂盒-植物_人Elisa试剂盒-PCR荧光定量试剂盒-上海一研生物科技有限公司 | 硫酸钡厂家_高光沉淀硫酸钡价格-河南钡丰化工有限公司 | 小型玉石雕刻机_家用玉雕机_小型万能雕刻机_凡刻雕刻机官网 | 【孔氏陶粒】建筑回填陶粒-南京/合肥/武汉/郑州/重庆/成都/杭州陶粒厂家 | 南京试剂|化学试剂|分析试剂|实验试剂|cas号查询-专业60年试剂销售企业 | 水厂污泥地磅|污泥处理地磅厂家|地磅无人值守称重系统升级改造|地磅自动称重系统维修-河南成辉电子科技有限公司 | 七维官网-水性工业漆_轨道交通涂料_钢结构漆| 制氮设备-变压吸附制氮设备-制氧设备-杭州聚贤气体设备制造有限公司 |