Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
82 stars 29 forks source link

Fixes error on Admin Edit Subscription page when the subscription has custom billing fields. #403

Closed mattallan closed 1 year ago

mattallan commented 1 year ago

Fixes #401 Fixes #402 Fixes https://github.com/woocommerce/woocommerce-subscriptions/issues/4468

Description

When a store has set custom address fields (using woocommerce_admin_billing_fields or woocommerce_admin_shipping_fields) and these fields don't have $order->get_billing_{field} or $order->get_shipping_{field} functions, opening the Edit subscription page in the admin will result in:

[31-Jan-2023 23:21:08 UTC] PHP Fatal error:  Uncaught Error: Call to undefined method WC_Subscription::get_billing_my_custom_field() in /Users/matt/local/woo/wp-content/plugins/woocommerce-subscriptions/vendor/woocommerce/subscriptions-core/includes/admin/meta-boxes/class-wcs-meta-box-subscription-data.php:216
Stack trace:
#0 /Users/matt/local/woo/wp-admin/includes/template.php(1401): WCS_Meta_Box_Subscription_Data::output(Object(WC_Subscription), Array)
#1 /Users/matt/local/woo/wp-admin/edit-form-advanced.php(688): do_meta_boxes(Object(WP_Screen), 'normal', Object(WP_Post))
#2 /Users/matt/local/woo/wp-admin/post.php(206): require('/Users/matt/loc...')
#3 /Users/matt/.composer/vendor/laravel/valet/server.php(235): require('/Users/matt/loc...')
#4 {main}
  thrown in /Users/matt/local/woo/wp-content/plugins/woocommerce-subscriptions/vendor/woocommerce/subscriptions-core/includes/admin/meta-boxes/class-wcs-meta-box-subscription-data.php on line 216

To fix this problem, we just need to make sure the function exists before we try to use it 😅 In cases where a getter function doesn't exist, we need to fallback to using $subscription->get_meta( '_custom_meta_key' )

How to test this PR

This issue was reported by a store using our EU Vat number plugin, however it can also be replicated by using the following snippet to add custom billing and shipping fields:

add_filter( 'woocommerce_admin_billing_fields', function( $fields ) {
    $fields['my_custom_field'] = array(
        'label' => 'My custom field'
    );

    return $fields;
}, 0 );

add_filter( 'woocommerce_admin_shipping_fields', function( $fields ) {
    $fields['my_custom_field'] = array(
        'label' => 'My custom field'
    );

    return $fields;
}, 0 );
  1. With this snippet, on trunk, try to navigate to the Edit Subscription page. You will notice half the page loaded and a fatal error.
  2. Check out this branch and confirm there's no fatal error.

Product impact

Jinksi commented 1 year ago

Will this resolve WCS issue https://github.com/woocommerce/woocommerce-subscriptions/issues/4468?

I'm attempting to test with https://github.com/woocommerce/woocommerce-eu-vat-number but I am seeing another fatal (unrelated I believe):

Fatal error: Uncaught Error: Call to undefined function wc_eu_vat_get_vat_from_order()
in [/var/www/html/wp-content/plugins/woocommerce-eu-vat-number/includes/class-wc-eu-vat-number.php](vscode://file/%2Fvar%2Fwww%2Fhtml%2Fwp-content%2Fplugins%2Fwoocommerce-eu-vat-number%2Fincludes%2Fclass-wc-eu-vat-number.php:568) on line 568

Update: It is unrelated:

WooCommerce EU VAT Number is inactive. Your server does not provide SOAP support which is required functionality for communicating with VIES. You will need to reach out to your web hosting provider to get information on how to enable this functionality on your server.
mattallan commented 1 year ago

@Jinksi yeah it will fix EU Vat Number which is using the woocommerce_admin_billing_fields filter.