且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在插入数据库之前使用挂钩从Woocommerce订单中删除费用

更新时间:2023-11-30 16:40:58

要在结帐后禁用订单中的费用,请使用以下简单的挂钩函数,该函数将清除订单和电子邮件通知中的所有费用:

To disable fees from order once checking out, use this simple following hooked function, that will clean all fees from orders and email notifications:

add_action( 'woocommerce_checkout_order_created', 'order_created_disable_fees' );
function order_created_disable_fees( $order ) {
    $targeted_item_name = __( "Total Tax Payment", "woocommerce" );

    foreach( $order->get_items( 'fee' ) as $item_id => $item ) {
        if( $targeted_item_name === $item['name'] ) {
            $order->remove_item($item_id);
        }       
    }
    $order->calculate_totals();
}

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

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

现在,如果您想取消费用,但保留原始总订单金额,请使用以下命令:

Now if you want to remove the fees, but keep original total order amount, use the following:

add_action( 'woocommerce_checkout_order_created', 'order_created_disable_fees' );
function order_created_disable_fees( $order ) {
    $targeted_item_name = __( "Total Tax Payment", "woocommerce" );

    foreach( $order->get_items( 'fee' ) as $item_id => $item ) {
        if( $targeted_item_name === $item['name'] ) {
            $order->remove_item($item_id);
        }
    }
    $order->save();
}

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

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