Automattic / woocommerce-payments

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

Using the global `wc` for configuration causes conflicts #7855

Closed RadoslavGeorgiev closed 6 months ago

RadoslavGeorgiev commented 9 months ago

Ticket: 7380387-zd-a8c

The getConfig function in checkout utils assumes that as long as a wc global is available, it's Woo configuration:

https://github.com/Automattic/woocommerce-payments/blob/4a667b85caa3f97572d055ca07107a51afd8201c/client/utils/checkout.js#L16-L18

However, it might not be. An extension I just stumbled upon does this in the global namespace:

var wc = document.getElementsByClassName('wc-tabs-wrapper');

In the given scenario, wc was not defined anyway, but because it's now an element, our code triggers a fatal error, and prevents payment request buttons from working, even though they show up.

To avoid this, we should first check whether wc.wcSettings exists, and that it has a getSetting() method.

zoupkat commented 9 months ago

In 7380387-zen, the user used the fix provided in this Slack thread by @RadoslavGeorgiev and resolved the incompatibility issue with the plugin Vertical Accordion and Product Detail tabs. TLDR: ApplePay&GooglePay buttons were not working in Single Product pages.

gpressutto5 commented 6 months ago

WooCommerce and other Extensions also rely on wc, so I couldn't simulate any scenario where the store is broken because of wc being overwritten and this check fixes it. I created a PR anyway so that we're doing our part in checking if an unpredictable variable that should never be overwritten holds the expected value. However, errors like this can only be solved by the extensions causing them or worked around using the suggested fix.