我认为您可以检测订单状态的变化,并将当前订单总额存储为负费用,以便在计算总额之前从订单总额中扣除。
为了减少混淆,代码应该是这样的:
add_action(\'woocommerce_order_status_changed\', \'add_credit_fee_if_editing\', 10,3);
function add_credit_fee_if_editing($order_id, $old_status, $new_status) {
$order = wc_get_order( $order_id );
$order_total = $order->get_total();
if($old_status == \'processing\' && $new_status == \'on-hold\') {//I am not sure about the \'on-hold\' status\'s slug so verify that for yourself.
$order->update_meta_data( \'_creditable_amount\', $order_total );
}
if($old_status == \'on-hold\' && $new_status == \'processing\') {//I am not sure about the \'on-hold\' status\'s slug so verify that for yourself.
$order_credit = $order->get_meta(\'_creditable_amount\', true);
if($order_total > $order_credit) {
$credit = -1 * $order_credit;
$order->add_fee(\'Credit\', $credit);
$order->calculate_totals();
$order->update_meta_data( \'_creditable_amount\', 0);
}
}
}
我认为这应该行得通。