通过 WooCommerce 中的管理员编辑订单自动添加或更新自定义费用 [英] Auto add or update a custom fee via admin edit orders in WooCommerce

查看:63
本文介绍了通过 WooCommerce 中的管理员编辑订单自动添加或更新自定义费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个特殊情况,我们会在收到订单后向客户开具付款发票,而不是让他们在结账时付款.运费由人工计算并添加到订单中,然后我们会在总额中添加 3% 的信用卡费用.

We have a special case where we invoice our customers for payment after the order has been received instead of having them pay during checkout. Shipping charges are manually calculated and added to the order and then we add a 3% credit card fee on the grand total.

为了自动执行此过程,我创建了一个脚本,该脚本在通过后端设置运费后计算 3% 的费用,并自动将此费用项目添加到订单中.当我们添加运费并点击保存/重新计算第一次时间时,这会起作用.

To automate this process, I created a script that calculates the 3% charge once the shipping charge has been set through the backend and adds this fee item into the order automatically. This works when we add the shipping charge and click save/recalculate the first time.

add_action( 'woocommerce_order_after_calculate_totals', "custom_order_after_calculate_totals", 10, 2);
function custom_order_after_calculate_totals($and_taxes, $order) {

    if ( did_action( 'woocommerce_order_after_calculate_totals' ) >= 2 )
    return;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 0.03;
    $total = $order->get_total();
    $surcharge = $total * $percentage;
    $feeArray = array(
        'name' => '3% CC Fee',
        'amount' =>  wc_format_decimal($surcharge),
        'taxable' => false,
        'tax_class' => ''
    );

    //Get fees
    $fees = $order->get_fees();
    if(empty($fees)){
        //Add fee
        $fee_object = (object) wp_parse_args( $feeArray );
        $order->add_fee($fee_object);
    } else {
        //Update fee
        foreach($fees as $item_id => $item_fee){
            if($item_fee->get_name() == "3% CC Fee"){
                $order->update_fee($item_id,$feeArray);
            }
        }
    }
}

如果我们不小心添加了错误的运费并尝试更新它,上面的代码会再次触发并更新费用,但是 $total 不会从更新的运费中获取新的订单总额所以费用不会改变.奇怪的是,如果我尝试删除费用项目,则会计算出新费用并添加回正确的费用金额.

If we accidentally add the wrong shipping cost and try to update it, this code above does get triggered again and updates the fee however $total does not get the new order total from the updated shipping cost and so the fee does not change. Strangely enough, if I try to delete the fee item, a new fee is calculated and is added back with the correct fee amount.

有人知道我如何解决这个问题吗?

Anybody know how I can solve this?

推荐答案

当你使用 order gran total 来计算你的费用并且你使用的钩子位于 calculate_totals() 方法中,一旦订单更新,您将始终需要按重新计算";按钮以获取正确的费用总额和正确金额的正确订单总额.

As you are using the order gran total to calculate your fee and as the hook you are using is located inside calculate_totals() method, once order get updated, you will always need to press "recalculate" button to get the correct fee total and the correct order gran total with the correct amounts.

自从 WooCommerce 3 以来,您的代码已经过时并且有点过时,并且有一些错误……例如 add_fee()update_fee() 方法已弃用并替换通过其他方式.

Since WooCommerce 3 your code is outdated and a bit obsolete with some mistakes… For example add_fee() and update_fee() methods are deprecated and replaced by some other ways.

改用以下内容:

add_action( 'woocommerce_order_after_calculate_totals', "custom_order_after_calculate_totals", 10, 2 );
function custom_order_after_calculate_totals( $and_taxes, $order ) {
    if ( did_action( 'woocommerce_order_after_calculate_totals' ) >= 2 )
        return;

    $percentage = 0.03; // Fee percentage

    $fee_data   = array(
        'name'       => __('3% CC Fee'),
        'amount'     => wc_format_decimal( $order->get_total() * $percentage ),
        'tax_status' => 'none',
        'tax_class'  => ''
    );

    $fee_items  = $order->get_fees(); // Get fees

    // Add fee
    if( empty($fee_items) ){
        $item = new WC_Order_Item_Fee(); // Get an empty instance object

        $item->set_name( $fee_data['name'] );
        $item->set_amount( $fee_data['amount'] );
        $item->set_tax_class($fee_data['tax_class']);
        $item->set_tax_status($fee_data['tax_status']);
        $item->set_total($fee_data['amount']);

        $order->add_item( $item );
        $item->save(); // (optional) to be sure
    }
    // Update fee
    else {
        foreach ( $fee_items as $item_id => $item ) {
            if( $item->get_name() === $fee_data['name'] ) {
                $item->set_amount($fee_data['amount']);
                $item->set_tax_class($fee_data['tax_class']);
                $item->set_tax_status($fee_data['tax_status']);
                $item->set_total($fee_data['amount']);
                $item->save();
            }
        }
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中.经测试有效.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

一旦订单更新并且按下重新计算按钮后 (以获得正确的订单总数)自动添加和更新的费用都会很好地工作.

Once order get updated and after press on recalculate button (to get the correct orders totals) both auto added and updated fee will work nicely.

相关:添加费用在 Woocommerce 3 中以编程方式下单

更新

现在如果它因任何原因不起作用,您应该删除相关项目以进行更新并添加一个新项目,如下所示:

Now if it doesn't work for any reason, you should remove the related item to update and add a new one as follows:

add_action( 'woocommerce_order_after_calculate_totals', "custom_order_after_calculate_totals", 10, 2 );
function custom_order_after_calculate_totals( $and_taxes, $order ) {
    if ( did_action( 'woocommerce_order_after_calculate_totals' ) >= 2 )
        return;

    $percentage = 0.03; // Fee percentage

    $fee_data   = array(
        'name'       => __('3% CC Fee'),
        'amount'     => wc_format_decimal( $order->get_total() * $percentage ),
        'tax_status' => 'none',
        'tax_class'  => ''
    );

    $fee_items  = $order->get_fees(); // Get fees

    // Add fee
    if( empty($fee_items) ){
        $item = new WC_Order_Item_Fee(); // Get an empty instance object

        $item->set_name( $fee_data['name'] );
        $item->set_amount( $fee_data['amount'] );
        $item->set_tax_class($fee_data['tax_class']);
        $item->set_tax_status($fee_data['tax_status']);
        $item->set_total($fee_data['amount']);

        $order->add_item( $item );
        $item->save(); // (optional) to be sure
    }
    // Update fee
    else {
        foreach ( $fee_items as $item_id => $item ) {
            if( $item->get_name() === $fee_data['name'] ) {
                $item->remove_item( $item_id ); // Remove the item

                $item = new WC_Order_Item_Fee(); // Get an empty instance object

                $item->set_name( $fee_data['name'] );
                $item->set_amount( $fee_data['amount'] );
                $item->set_tax_class($fee_data['tax_class']);
                $item->set_tax_status($fee_data['tax_status']);
                $item->set_total($fee_data['amount']);

                $order->add_item( $item );
                $item->save(); // (optional) to be sure
            }
        }
    }
}

这篇关于通过 WooCommerce 中的管理员编辑订单自动添加或更新自定义费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆