Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged

Conversation

mattallan
Copy link
Contributor

@mattallan mattallan commented Jan 31, 2023

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

  • Added changelog entry (or does not apply)
  • Will this PR affect WooCommerce Subscriptions? yes/no/tbc, add issue ref
  • Will this PR affect WooCommerce Payments? yes/no/tbc, add issue ref
  • Added deprecated functions, hooks or classes to the spreadsheet

@mattallan mattallan requested a review from a team January 31, 2023 23:00
@Jinksi Jinksi self-requested a review January 31, 2023 23:03
@Jinksi
Copy link
Member

Jinksi commented Jan 31, 2023

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
Copy link
Contributor Author

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

Copy link
Member

@Jinksi Jinksi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @mattallan 🏆

I've tested by using snippets from the description (with WCPay standalone and WCPay + WCS).

  • On trunk, I see the error when loading the Edit Subscription screen.
  • On this branch, the Edit Subscription screen loads correctly and I can edit the custom billing/shipping fields and view the stored value.

@mattallan mattallan merged commit 2af5456 into trunk Feb 1, 2023
@mattallan mattallan deleted the fix/custom-billing-fields-breaks-edit-subsciption-page branch February 1, 2023 00:04
mattallan added a commit that referenced this pull request Apr 17, 2023
… custom billing fields. (#403)

* Checks if get_billing_{key} function exists and falls back to getting meta

* Update includes/admin/meta-boxes/class-wcs-meta-box-subscription-data.php

* Call is_callable() when getting shipping field value, fallback to using get_meta()

* changelog entry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants