wc_order_fully_refunded()
When refunding an order, create a refund line item if the partial refunds do not match order total.
This is manual; no gateway refund will be performed.
No Hooks.
Returns
null. Nothing (null).
Usage
wc_order_fully_refunded( $order_id );
- $order_id(int) (required)
- Order ID.
Changelog
| Since 2.4 | Introduced. |
wc_order_fully_refunded() wc order fully refunded code WC 11.1.2
function wc_order_fully_refunded( $order_id ) {
$order = wc_get_order( $order_id );
$max_refund = wc_format_decimal( $order->get_total() - $order->get_total_refunded() );
// Numeric comparison, not truthiness: when prior refunds sum a float epsilon
// above the order total, the difference formats to '-0' — a truthy string that
// is numerically zero — which would otherwise create a ghost 0.00 refund below.
if ( (float) $max_refund <= 0 ) {
return;
}
// Create the refund object.
wc_switch_to_site_locale();
wc_create_refund(
array(
'amount' => $max_refund,
'reason' => __( 'Order fully refunded.', 'woocommerce' ),
'order_id' => $order_id,
'line_items' => array(),
)
);
wc_restore_locale();
$order->add_order_note( __( 'Order status set to refunded. To return funds to the customer you will need to issue a refund through your payment gateway.', 'woocommerce' ), false, false, array( 'note_group' => OrderNoteGroup::ORDER_UPDATE ) );
}