duancanjiu3754 2018-02-28 10:46
浏览 254
已采纳

在Woocommerce结账后更改订单总额

I cant seem to find which hook to use to change the total (or any variable of the cart) after user clicks checkout. So for example, user Submits the Checkout form and then I need to do some checks and change the total accordingly.

How should I do that, which hook do I use?

  • 写回答

1条回答 默认 最新

  • doufenpaiyu63706 2018-02-28 15:32
    关注

    This can be done in woocommerce_checkout_create_order action hook, where you will have to use CRUD getters and setters methods for WC_Abstract_Order and WC_Order classes...

    As cart object and cart session has not been destroyed yet, you can still also use WC()->cart object and WC_Cart methods, to get data…

    This hook is triggered just before the order data is saved in database with $order->save();. You can see that in the source code HERE.

    Below a fake working example:

    add_action( 'woocommerce_checkout_create_order', 'change_total_on_checking', 20, 1 );
    function change_total_on_checking( $order ) {
        // Get order total
        $total = $order->get_total();
    
        ## -- Make your checking and calculations -- ##
        $new_total = $total * 1.12; // <== Fake calculation
    
        // Set the new calculated total
        $order->set_total( $new_total );
    }
    

    Code goes in function.php file of your active child theme (or theme).

    Tested and works.

    Some explanations here: Add extra meta for orders in Woocommerce

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 怎么实现数组的循环累加,simulink
  • ¥15 51单片机最小开发板系统,想让寻迹小车在全检测到黑线(寻迹模块代码在第一块板子上)时蜂鸣器响(在第二块板子上)
  • ¥15 pbootcms多选调用成列表
  • ¥15 51单片机oled显示时钟
  • ¥15 小规模TSP问题的动态规划求解
  • ¥25 kubelet.service: Failed with result 'exit-code'.
  • ¥15 bitvise黑框内一键部署v2ray提示账户没有root怎么解决
  • ¥15 车型识别以及相似度匹配中细节特征提取以及图像模糊问题
  • ¥15 怎么用鸿蒙的ArkTs写出来啊
  • ¥30 websocket服务端多线程通信
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部