Automattic\WooCommerce\Internal
RestApiParameterUtil::calculate_refund_amount_from_line_items
Calculate the "amount" parameter for the request based on the amounts found in line items. This will ONLY be possible if ALL of the following is true:
- "line_items" in the request is a non-empty array.
- All line items have a "refund_total" field with a numeric value.
- All values inside "refund_tax" in all line items are a numeric value.
The request is assumed to be in internal format already.
Method of the class: RestApiParameterUtil{}
No Hooks.
Returns
number|null. The calculated amount, or null if it can't be calculated.
Usage
$result = RestApiParameterUtil::calculate_refund_amount_from_line_items( $request );
- $request(WP_REST_Request) (required)
- The request to maybe calculate the total amount for.
RestApiParameterUtil::calculate_refund_amount_from_line_items() RestApiParameterUtil::calculate refund amount from line items code WC 10.3.3
private static function calculate_refund_amount_from_line_items( $request ) {
$line_items = $request['line_items'];
if ( ! is_array( $line_items ) || empty( $line_items ) ) {
return null;
}
$amount = 0;
foreach ( $line_items as $item ) {
if ( ! isset( $item['refund_total'] ) || ! is_numeric( $item['refund_total'] ) ) {
return null;
}
$amount += $item['refund_total'];
if ( ! isset( $item['refund_tax'] ) ) {
continue;
}
foreach ( $item['refund_tax'] as $tax ) {
if ( ! is_numeric( $tax ) ) {
return null;
}
$amount += $tax;
}
}
return $amount;
}