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

Ensure WCPay JS loaded on Checkout page for zero order totals #5423

Merged
merged 12 commits into from
Feb 23, 2023

Conversation

jessy-p
Copy link
Contributor

@jessy-p jessy-p commented Jan 19, 2023

When an item with price 0 is added to the cart, it is expected that the payment form does not need to be displayed on the Checkout page. But, when the cart is updated via AJAX on the checkout page to be non-zero, the payment form should appear, because it is no longer a 0 priced order.

Fixes #4764
Fixes #5277

Changes proposed in this Pull Request

  • Even when cart is zero, make sure the JS is loaded.
  • Do this only on checkout page, and not on blocks page because the Blocks Checkout already loads all the required JS.

Testing instructions

  1. Test with steps in Payment form absent when item with price added to cart via AJAX on a free order #4764
  2. Test with steps in Display CC fields when a 100% coupon code that is added in cart is removed on checkout page #5277
  3. Test with Checkout Blocks and UPE (all variations of checkout) and on other pages where the checkout form is used e.g. Pay for Order, My Accounts etc to ensure no regression
  4. Can test with any plugins that update order via AJAX on checkout page.

  • Run npm run changelog to add a changelog file, choose patch to leave it empty if the change is not significant. You can add multiple changelog files in one PR by running this command a few times.
  • Covered with tests (or have a good reason not to test in description ☝️)
  • Tested on mobile (or does not apply)

Post merge

Jessy P added 2 commits January 19, 2023 09:34
that when order is updated via ajax from Checkout pages, payment form
loads.
@jessy-p jessy-p self-assigned this Jan 19, 2023
@jessy-p jessy-p requested a review from a team January 19, 2023 04:12
@github-actions
Copy link
Contributor

github-actions bot commented Jan 19, 2023

Size Change: 0 B

Total Size: 1.37 MB

ℹ️ View Unchanged
Filename Size
release/woocommerce-payments/assets/css/admin.css 913 B
release/woocommerce-payments/assets/css/success.css 401 B
release/woocommerce-payments/dist/blocks-checkout.css 2.33 kB
release/woocommerce-payments/dist/blocks-checkout.js 39.6 kB
release/woocommerce-payments/dist/checkout.css 1.34 kB
release/woocommerce-payments/dist/checkout.js 28.1 kB
release/woocommerce-payments/dist/index.css 36.1 kB
release/woocommerce-payments/dist/index.js 384 kB
release/woocommerce-payments/dist/multi-currency-analytics.js 1.05 kB
release/woocommerce-payments/dist/multi-currency-switcher-block.js 56.3 kB
release/woocommerce-payments/dist/multi-currency.css 14.8 kB
release/woocommerce-payments/dist/multi-currency.js 72.7 kB
release/woocommerce-payments/dist/order.css 248 B
release/woocommerce-payments/dist/order.js 12.7 kB
release/woocommerce-payments/dist/payment-gateways.css 1.2 kB
release/woocommerce-payments/dist/payment-gateways.js 47.2 kB
release/woocommerce-payments/dist/payment-request.js 12 kB
release/woocommerce-payments/dist/platform-checkout-express-button.js 16 kB
release/woocommerce-payments/dist/platform-checkout.css 4.03 kB
release/woocommerce-payments/dist/platform-checkout.js 75.5 kB
release/woocommerce-payments/dist/settings.css 51.3 kB
release/woocommerce-payments/dist/settings.js 181 kB
release/woocommerce-payments/dist/subscription-edit-page.js 668 B
release/woocommerce-payments/dist/subscription-product-onboarding-modal.css 7.18 kB
release/woocommerce-payments/dist/subscription-product-onboarding-modal.js 12.8 kB
release/woocommerce-payments/dist/subscription-product-onboarding-toast.js 703 B
release/woocommerce-payments/dist/subscriptions-empty-state.css 298 B
release/woocommerce-payments/dist/subscriptions-empty-state.js 44.3 kB
release/woocommerce-payments/dist/tos.css 236 B
release/woocommerce-payments/dist/tos.js 15.7 kB
release/woocommerce-payments/dist/upe_checkout.css 1.34 kB
release/woocommerce-payments/dist/upe_checkout.js 32.9 kB
release/woocommerce-payments/dist/upe_split_checkout.css 1.34 kB
release/woocommerce-payments/dist/upe_split_checkout.js 33.2 kB
release/woocommerce-payments/dist/upe-blocks-checkout.css 2.33 kB
release/woocommerce-payments/dist/upe-blocks-checkout.js 39.1 kB
release/woocommerce-payments/dist/upe-split-blocks-checkout.css 2.34 kB
release/woocommerce-payments/dist/upe-split-blocks-checkout.js 39.7 kB
release/woocommerce-payments/includes/subscriptions/assets/css/plugin-page.css 633 B
release/woocommerce-payments/includes/subscriptions/assets/js/plugin-page.js 720 B
release/woocommerce-payments/vendor/automattic/jetpack-admin-ui/src/css/jetpack-icon.css 224 B
release/woocommerce-payments/vendor/automattic/jetpack-assets/build/i18n-loader.js 2.43 kB
release/woocommerce-payments/vendor/automattic/jetpack-assets/src/js/i18n-loader.js 1.01 kB
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/babel.config.js 160 B
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.css 2.27 kB
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.js 13.3 kB
release/woocommerce-payments/vendor/automattic/jetpack-identity-crisis/build/index.rtl.css 2.27 kB
release/woocommerce-payments/vendor/automattic/jetpack-tracking/src/js/tracks-ajax.js 789 B
release/woocommerce-payments/vendor/automattic/jetpack-tracking/src/js/tracks-callables.js 925 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/about.css 1.2 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin-order-statuses.css 403 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/admin.css 3.56 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/checkout.css 299 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/modal.css 742 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/view-subscription.css 572 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/css/wcs-upgrade.css 411 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/admin-pointers.js 544 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/admin.js 9.07 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/jstz.js 6.8 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/jstz.min.js 3.83 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/meta-boxes-coupon.js 544 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/meta-boxes-subscription.js 2.38 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/moment.js 22.1 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/moment.min.js 11.6 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/payment-method-restrictions.js 1.29 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/admin/wcs-meta-boxes-order.js 502 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/payment-methods.js 355 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/single-product.js 429 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/view-subscription.js 1.38 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/frontend/wcs-cart.js 387 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/modal.js 1.1 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/assets/js/wcs-upgrade.js 1.27 kB
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/build/index.css 392 B
release/woocommerce-payments/vendor/woocommerce/subscriptions-core/build/index.js 3.06 kB

compressed-size-action

@jessy-p
Copy link
Contributor Author

jessy-p commented Jan 19, 2023

Working only for non-UPE payment method, need to fix for UPE and test for block checkout and other checkout combinations.

jessy-p and others added 3 commits February 17, 2023 11:50
the cart is updated via AJAX on checkout page.
and not in blocks checkout page as the latter already loads
required scripts even when cart is free.
@jessy-p
Copy link
Contributor Author

jessy-p commented Feb 17, 2023

Fixed for UPE and blocks too. Ready for review @Automattic/gamma

Copy link
Contributor

@fashuvo fashuvo left a comment

Choose a reason for hiding this comment

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

Tested this locally. The fix works for Classic checkout and Blocks checkout, but it isn't working for UPE.

Copy link
Contributor

@fashuvo fashuvo left a comment

Choose a reason for hiding this comment

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

Changes LGTM. Works on UPE, Classic checkout, and Blocks checkout.

Copy link
Member

@htdat htdat left a comment

Choose a reason for hiding this comment

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

While reviewing this PR #5652, I did have a look at this issue too. All of these inline comments are more ideas, they're not meant to update/fix the code right away.

@@ -629,6 +629,12 @@ public function register_scripts() {
);

wp_set_script_translations( 'WCPAY_CHECKOUT', 'woocommerce-payments' );

if ( ! WC()->cart->needs_payment() && is_checkout() && ! has_block( 'woocommerce/checkout' ) ) {
WC_Payments::get_gateway()->tokenization_script();
Copy link
Member

Choose a reason for hiding this comment

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

We may just directly $this->tokenization_script() as it's basically the same one but without calling an outside function to get another inside function.

@@ -629,6 +629,12 @@ public function register_scripts() {
);

wp_set_script_translations( 'WCPAY_CHECKOUT', 'woocommerce-payments' );

if ( ! WC()->cart->needs_payment() && is_checkout() && ! has_block( 'woocommerce/checkout' ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: moving is_checkout() as the first check as it can help terminate this code for most cases, which results in a little bit faster processing in general.

Comment on lines +139 to +142
if ( ! WC()->cart->needs_payment() && is_checkout() && ! has_block( 'woocommerce/checkout' ) ) {
WC_Payments::get_gateway()->tokenization_script();
WC_Payments::get_wc_payments_checkout()->enqueue_payment_scripts();
}
Copy link
Member

Choose a reason for hiding this comment

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

IMO, this whole block can be moved to a method in WC_Payment_Gateway_WCPay and then you can use that method for all of these places. Without that, we do not need to write the same code in three places. We can use this because of the inheritance:

  • UPE_Payment_Gateway extends WC_Payment_Gateway_WCPay
  • UPE_Split_Payment_Gateway extends UPE_Payment_Gateway

Another possibility is to just hook this extracted method into the wp_enqueue_scripts filter. With that, you just need to write this code one time in the top-level class WC_Payment_Gateway_WCPay.

Something like this: https://gist.github.com/htdat/679c224b1c5a761be6082e44695b76ee

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants