Automattic / woocommerce-payments

Accept payments via credit card. Manage transactions within WordPress.
https://wordpress.org/plugins/woocommerce-payments/
Other
165 stars 67 forks source link

Get Store currency from WooCommerce Settings instead of `wcpay_multi_currency_store_currency` #8879

Open csmcneill opened 1 month ago

csmcneill commented 1 month ago

Originally reported in https://github.com/woocommerce/woocommerce/issues/47793 — moving here since the issue seems to be rooted in WooPayments (cc @AashikP @kaushikasomaiya)


Describe the bug

If you use a custom currency, and then remove the code declaration for that custom currency, WooPayments will throw a Fatal Error.

To Reproduce

  1. Create a custom currency
add_filter( 'woocommerce_currencies', 'add_my_currency' );

function add_my_currency( $currencies ) {
     $currencies['SING'] = __( 'Singa Custom', 'woocommerce' );
     return $currencies;
}
  1. Set that as the currency under WooCommerce → Settings → General → Currency options
  2. Remove the custom code.
  3. WooPayments will then throw this error:
24-May-2024 06:52:37 UTC] PHP Fatal error:  Uncaught TypeError: WCPay\MultiCurrency\Currency::get_name(): Return value must be of type string, null returned in /wordpress/plugins/woocommerce-payments/7.6.0/includes/multi-currency/Currency.php:156
Stack trace:
#0 /wordpress/plugins/woocommerce-payments/7.6.0/includes/multi-currency/Analytics.php(140): WCPay\MultiCurrency\Currency->get_name()
#1 /wordpress/plugins/woocommerce-payments/7.6.0/includes/multi-currency/Analytics.php(63): WCPay\MultiCurrency\Analytics->register_customer_currencies()
#2 /wordpress/plugins/woocommerce-payments/7.6.0/includes/multi-currency/Analytics.php(52): WCPay\MultiCurrency\Analytics->init()
#3 /wordpress/plugins/woocommerce-payments/7.6.0/includes/multi-currency/MultiCurrency.php(286): WCPay\MultiCurrency\Analytics->__construct(Object(WCPay\MultiCurrency\MultiCurrency))
#4 /wordpress/core/6.5.3/wp-includes/class-wp-hook.php(324): WCPay\MultiCurrency\MultiCurrency->init('')
#5 /wordpress/core/6.5.3/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#6 /wordpress/core/6.5.3/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#7 /wordpress/core/6.5.3/wp-settings.php(695): do_action('init')
#8 /srv/htdocs/wp-config.php(83): require_once('/wordpress/core...')
#9 /wordpress/core/6.5.3/wp-load.php(55): require_once('/srv/htdocs/wp-...')
#10 /wordpress/core/6.5.3/wp-blog-header.php(13): require_once('/wordpress/core...')
#11 /wordpress/core/6.5.3/index.php(17): require('/wordpress/core...')
#12 {main}
  thrown in /wordpress/plugins/woocommerce-payments/7.6.0/includes/multi-currency/Currency.php on line 156
  1. To fix this, you either need to add the currency declaration back or save a different currency under WooCommerce → Settings → General → Currency options

Actual behavior

WooPayments relies on the wcpay_multi_currency_store_currency database entry to guess the custom currency.

Screenshots

Expected behavior

WooCommerce itself reverts back to the first currency in the list if the code declaration for the custom currency is not available. WooPayments should either use that logic or look at store currency.

Additional context

p1716534045942739-slack-C3NCP7ZJ6 p1692737131166069-slack-C3NCP7ZJ6

csmcneill commented 1 month ago

Some additional information:

  • Merchant likely added custom currency via a plugin or code - Singapore_Custom
  • WooPayments saves the ID code of the custom currency in DB and uses get_woocommerce_currencies() core method to retrieve name of the currency using that saved ID
  • Important part is that if the custom currency code declaration is removed - WooPayment will throw an error because get_woocommerce_currencies() will no longer include the custom currency in the array and function get_name() in WooPayments returns a blank. (error thrown because of type declaration string)
  • We don't know yet if it was added via code or plugin but basically should yield same results.
AashikP commented 1 month ago

Thank you for moving this to the right repo, @csmcneill!