WooCommerce – wc_create_refund – get_shipping_country

I’m setting up a reseller store and developing a hook that sends a request off to an API to purchase the product, if the API is broken or the product is out of stock I’d like the customer refunded automatically.

I’m running into an issue with wc_create_refund, note that when you place an order I don’t ask for address information as it’s not required for my store and I currently don’t calculate tax. I get the following fatal error when running the function –

[17-Oct-2023 09:28:39 UTC] PHP Fatal error:  Uncaught Error: Call to undefined method WC_Order_Refund::get_shipping_country() in D:wamp64wwwwp-contentpluginswoocommerceincludesabstractsabstract-wc-order.php:1660
Stack trace:
#0 D:wamp64wwwwp-contentpluginswoocommerceincludesabstractsabstract-wc-order.php(1747): WC_Abstract_Order->get_tax_location(Array)
#1 D:wamp64wwwwp-contentpluginswoocommerceincludesabstractsabstract-wc-order.php(1921): WC_Abstract_Order->calculate_taxes()
#2 D:wamp64wwwwp-contentthemeswoodmart-childtestorder.php(162): WC_Abstract_Order->calculate_totals()
#3 D:wamp64wwwwp-contentthemeswoodmart-childtestorder.php(92): refund_user_for_failed_order(Object(WC_Product_Simple), Array, 32097)
#4 D:wamp64wwwwp-contentthemeswoodmart-childtestorder.php(345): create_api_order(32097)
#5 {main}
  thrown in D:wamp64wwwwp-contentpluginswoocommerceincludesabstractsabstract-wc-order.php on line 1660

Here’s my function

// Refund Function
function refund_user_for_failed_order($product, $response, $order_id) {
    // Get the product price for the refund amount (adjust this based on your pricing structure)
    $refund_amount = $product->get_price();
    error_log('Refund Amount: ' . $refund_amount);
    error_log('Product Price: ' . $product->get_price());

    // Get the user associated with the order (you may need to customize this part)
    $user_id = get_current_user_id(); // For demonstration purposes, assuming the current user is the buyer

    // Provide default shipping details for digital products
    $default_shipping_data = array(
        'country' => 'AU', // Provide the appropriate country code
        'state' => 'NSW', // Provide the appropriate state code
        'postcode' => '2000', // Provide the appropriate postcode
        'city' => 'Sydney', // Provide the appropriate city
    );

    // Check if the user is eligible for a refund (you may have specific refund policies)
    if ($user_id && $refund_amount > 0) {
        // Load the original order
        $order = wc_get_order($order_id);

        // Create a refund request
        $refund = wc_create_refund(
            array(
                'amount' => $refund_amount,
                'reason' => 'Failed eSIM order', // Customize this reason
                'order_id' => $order_id, // Use the product's parent order ID
                'line_items' => array(
                    array(
                        'product_id' => $product->get_id(),
                        'variation_id' => 0, // Assuming no variation
                        'quantity' => 1,
                    ),
                ),
                'shipping' => $default_shipping_data, // Provide default shipping data
            )
        );

        // Check if the refund was created successfully
        if (is_wp_error($refund)) {
            // Handle the error, log it and notify the administrator
            error_log('Failed to create refund: ' . $refund->get_error_message());
            echo 'An error has occured and you will be refunded for this order - Website admins have been notified.';          
            // Send an email to [email protected]
            $subject = 'Refund Un-Successful';
            $message = 'Refund Un-Successful for Product Id ' . $product->get_id();
            send_email('[email protected]', $subject, $message);            
        } else {
            // Process the refund
            $refund->calculate_totals(); // Calculate totals
            $refund->refund(); // Perform the refund
            echo 'An error has occured and you will be refunded for this order - Website admins have been notified.';

            // notify the administrator about a successful refund
            error_log('Refund successful for product ID: ' . $product->get_id());
            // Send an email to [email protected]
            $subject = 'Refund Successful';
            $message = 'Refund Successful for Product Id ' . $product->get_id();
            send_email('[email protected]', $subject, $message);
        }
    }
}

It actually processes the refund, I just get this nasty error that I’d like to somehow bypass,

Thanks!

I’ve tried to specify dummy shipping data, this didn’t change anything and I’ve tried to specify tax to be 0 but it’s a protected method.