WooCommerce icon indicating copy to clipboard operation
WooCommerce copied to clipboard

Refunds not working, Fatal error: Uncaught TypeError: abs() in src/Payment/RefundLineItemsBuilder.php:73

Open Fidelity88 opened this issue 4 weeks ago • 0 comments

When trying to do a refund on some orders. It's not working and give a fatal error:

[28-Jan-2025 15:19:19 UTC] PHP Fatal error:  Uncaught TypeError: abs(): Argument #1 ($num) must be of type int|float, string given in /wp-content/plugins/mollie-payments-for-woocommerce/src/Payment/RefundLineItemsBuilder.php:71
Stack trace:
#0 /wp-content/plugins/mollie-payments-for-woocommerce/src/Payment/RefundLineItemsBuilder.php(71): abs()
#1 /wp-content/plugins/mollie-payments-for-woocommerce/src/Payment/RefundLineItemsBuilder.php(49): Mollie\WooCommerce\Payment\RefundLineItemsBuilder->buildLineItem()
#2 /wp-content/plugins/mollie-payments-for-woocommerce/src/Payment/OrderItemsRefunder.php(64): Mollie\WooCommerce\Payment\RefundLineItemsBuilder->buildLineItems()
#3 /wp-content/plugins/mollie-payments-for-woocommerce/src/Payment/MollieOrder.php(463): Mollie\WooCommerce\Payment\OrderItemsRefunder->refund()
#4 /wp-content/plugins/mollie-payments-for-woocommerce/src/Gateway/MolliePaymentGateway.php(524): Mollie\WooCommerce\Payment\MollieOrder->refund()
#5 /wp-content/plugins/woocommerce/includes/wc-order-functions.php(773): Mollie\WooCommerce\Gateway\MolliePaymentGateway->process_refund()
#6 /wp-content/plugins/woocommerce/includes/wc-order-functions.php(667): wc_refund_payment()
#7 /wp-content/plugins/woocommerce/includes/class-wc-ajax.php(2205): wc_create_refund()
#8 /wp-includes/class-wp-hook.php(324): WC_AJAX::refund_line_items()
#9 /wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#10 /wp-includes/plugin.php(517): WP_Hook->do_action()
#11 /wp-admin/admin-ajax.php(192): do_action()
#12 {main}
  thrown in /wp-content/plugins/mollie-payments-for-woocommerce/src/Payment/RefundLineItemsBuilder.php on line 71

The problem seems to lie within this line: https://github.com/mollie/WooCommerce/blob/c2981ac834e2cf1328f57fded4a2703ce54501ba/src/Payment/RefundLineItemsBuilder.php#L108 $toRefundItemQuantity = abs($toRefundItem->get_quantity());

When I var_dump the output of $toRefundItem->get_quantity(), the output is string(5) "-100." . I don't know if this is expected when you enter "100" as refund quantity. I certainly did not type a dot after the value.

When I check the output of $toRefundItem, I get this result:

(
    [id:protected] => 34362
    [data:protected] => Array
        (
            [order_id] => 40786
            [name] => PTFE slang 1 mm x 2 mm
            [product_id] => 9475
            [variation_id] => 0
            [quantity] => -100.
            [tax_class] =>
            [subtotal] => -3.6
            [subtotal_tax] => -0.76
            [total] => -3.6
            [total_tax] => -0.76
[...]

So for some reason. The quantity refund breaks on this.

The workaround that fixed the problem for me was to add (int) inside the abs() function. So change this: $toRefundItemQuantity = abs($toRefundItem->get_quantity());

To this: $toRefundItemQuantity = abs((int) $toRefundItem->get_quantity());

I can now perform refunds in WooCommerce again.

Plugin version: 7.9.17.9.1

Fidelity88 avatar Jan 28 '25 16:01 Fidelity88