Hey @yousefyousef,
Apologies – I completely missed this. This sounds more like a generic WooCommerce question but I think I know how to solve it. I would suggest installing the Code Snippets plugin and using the snippet below. Before you do this, please make sure that your site is fully backed up. Also I would recommend that you run this in a test environment first before using this in production.
add_action('woocommerce_order_status_changed', 'auto_refund_on_cancelled_for_swish', 10, 3);
function auto_refund_on_cancelled_for_swish($order_id, $old_status, $new_status) {
if ($new_status === 'cancelled') {
$order = wc_get_order($order_id);
if ($order && $order->get_payment_method() === 'swish') {
$total = $order->get_total();
$refunded = $order->get_total_refunded();
$remaining = $total - $refunded;
if ($remaining > 0) {
$refund = wc_create_refund(array(
'amount' => $remaining,
'reason' => 'Order automatically refunded on cancellation',
'order_id' => $order_id,
'refund_payment' => true,
));
if (is_wp_error($refund)) {
error_log('Refund failed: ' . $refund->get_error_message());
}
}
}
}
}
/Carl