Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Experiment: Add available_payment_methods to the API and cart data #5442

Closed
wants to merge 1 commit into from

Conversation

mikejolley
Copy link
Member

This attempts to solve the problem in #5388 by adding available_payment_methods to the cart response data with the current list of available payment method IDs.

Payment methods are registered if enabled in settings. This enqueues the scripts for Blocks.

This script is not aware of the woocommerce_available_payment_gateways filter, so to handle this we need to check if a method is available via its canMakePayment callback.

So canMakePayment knows which methods are available, I've updated the cart data to include a list of available payment method IDs which are surfaced to gateways.

Testing

Use a snippet like this:

add_filter(
	'woocommerce_available_payment_gateways',
	function( $methods ) {
		if ( wc()->cart->get_total( 'edit' ) < 100 ) {
			unset( $methods['bacs'] );
		}
		return $methods;
	}
);

Make a free shipping and flat rate shipping method costing $50. Add $60 of items to the cart. Toggle between free shipping and flat rate shipping so the totals change. BACS should only be available when free shipping is checked.

@mikejolley mikejolley self-assigned this Dec 22, 2021
@mikejolley mikejolley marked this pull request as draft December 22, 2021 16:17
@mikejolley mikejolley added focus: rest api Work impacting REST api routes. block: checkout Issues related to the checkout block. labels Dec 22, 2021
@rubikuserbot rubikuserbot requested review from a team and nielslange and removed request for a team December 22, 2021 16:17
@mikejolley mikejolley requested a review from senadir December 22, 2021 16:18
@github-actions
Copy link
Contributor

Size Change: +474 B (0%)

Total Size: 819 kB

Filename Size Change
build/active-filters-frontend.js 6.22 kB +5 B (0%)
build/all-products-frontend.js 18.7 kB +42 B (0%)
build/all-products.js 34.4 kB +38 B (0%)
build/atomic-block-components/add-to-cart--atomic-block-components/button--atomic-block-components/image---a7e2bb9b.js 2.8 kB +34 B (+1%)
build/atomic-block-components/category-list-frontend.js 458 B +1 B (0%)
build/atomic-block-components/tag-list-frontend.js 459 B +1 B (0%)
build/cart-blocks/express-payment-frontend.js 4.86 kB -1 B (0%)
build/cart-blocks/order-summary-frontend.js 8.95 kB +2 B (0%)
build/cart-frontend.js 45.6 kB +28 B (0%)
build/cart.js 44.3 kB +45 B (0%)
build/checkout-blocks/express-payment-frontend.js 5.15 kB -1 B (0%)
build/checkout-frontend.js 47.7 kB +50 B (0%)
build/checkout.js 47.1 kB +40 B (0%)
build/mini-cart-component-frontend.js 14.2 kB +31 B (0%)
build/single-product-frontend.js 22.2 kB +37 B (0%)
build/single-product.js 10.4 kB -2 B (0%)
build/stock-filter-frontend.js 6.81 kB -1 B (0%)
build/wc-blocks-data.js 8.86 kB +18 B (0%)
build/wc-payment-method-bacs.js 846 B +26 B (+3%)
build/wc-payment-method-cheque.js 842 B +26 B (+3%)
build/wc-payment-method-cod.js 935 B +23 B (+3%)
build/wc-payment-method-paypal.js 870 B +32 B (+4%)
ℹ️ View Unchanged
Filename Size
build/active-filters.js 7.05 kB
build/all-reviews.js 8.35 kB
build/atomic-block-components/add-to-cart--atomic-block-components/button.js 1.48 kB
build/atomic-block-components/add-to-cart-frontend.js 6.87 kB
build/atomic-block-components/add-to-cart.js 6.42 kB
build/atomic-block-components/button-frontend.js 1.48 kB
build/atomic-block-components/button.js 851 B
build/atomic-block-components/category-list.js 458 B
build/atomic-block-components/image-frontend.js 1.37 kB
build/atomic-block-components/image.js 1.05 kB
build/atomic-block-components/price-frontend.js 1.74 kB
build/atomic-block-components/price.js 1.7 kB
build/atomic-block-components/rating-frontend.js 552 B
build/atomic-block-components/rating.js 554 B
build/atomic-block-components/sale-badge-frontend.js 625 B
build/atomic-block-components/sale-badge.js 622 B
build/atomic-block-components/sku-frontend.js 386 B
build/atomic-block-components/sku.js 385 B
build/atomic-block-components/stock-indicator-frontend.js 584 B
build/atomic-block-components/stock-indicator.js 585 B
build/atomic-block-components/summary-frontend.js 872 B
build/atomic-block-components/summary.js 871 B
build/atomic-block-components/tag-list.js 458 B
build/atomic-block-components/title-frontend.js 1.11 kB
build/atomic-block-components/title.js 1.1 kB
build/attribute-filter-frontend.js 16.3 kB
build/attribute-filter.js 12.6 kB
build/blocks-checkout.js 17.6 kB
build/cart-blocks/accepted-payment-methods-frontend.js 1.15 kB
build/cart-blocks/checkout-button-frontend.js 1.14 kB
build/cart-blocks/empty-cart-frontend.js 345 B
build/cart-blocks/filled-cart-frontend.js 766 B
build/cart-blocks/items-frontend.js 298 B
build/cart-blocks/line-items-frontend.js 5.13 kB
build/cart-blocks/totals-frontend.js 320 B
build/checkout-blocks/actions-frontend.js 1.44 kB
build/checkout-blocks/billing-address--checkout-blocks/shipping-address-frontend.js 4.22 kB
build/checkout-blocks/billing-address-frontend.js 884 B
build/checkout-blocks/contact-information-frontend.js 2.94 kB
build/checkout-blocks/fields-frontend.js 343 B
build/checkout-blocks/order-note-frontend.js 1.13 kB
build/checkout-blocks/order-summary-frontend.js 11.4 kB
build/checkout-blocks/payment-frontend.js 7.41 kB
build/checkout-blocks/shipping-address-frontend.js 971 B
build/checkout-blocks/shipping-methods-frontend.js 4.81 kB
build/checkout-blocks/terms-frontend.js 1.21 kB
build/checkout-blocks/totals-frontend.js 324 B
build/featured-category.js 8.55 kB
build/featured-product.js 9.9 kB
build/handpicked-products.js 7.32 kB
build/legacy-template.js 2.08 kB
build/mini-cart-contents.js 3.59 kB
build/mini-cart-frontend.js 1.76 kB
build/mini-cart.js 6.46 kB
build/price-filter-frontend.js 12.4 kB
build/price-filter.js 8.6 kB
build/price-format.js 1.18 kB
build/product-best-sellers.js 7.51 kB
build/product-categories.js 3.47 kB
build/product-category.js 8.36 kB
build/product-new.js 7.66 kB
build/product-on-sale.js 8.05 kB
build/product-search.js 2.47 kB
build/product-tag.js 7.76 kB
build/product-top-rated.js 7.63 kB
build/products-by-attribute.js 8.48 kB
build/reviews-by-category.js 11.9 kB
build/reviews-by-product.js 12.9 kB
build/reviews-frontend.js 7.25 kB
build/stock-filter.js 6.82 kB
build/vendors--atomic-block-components/add-to-cart--cart-blocks/order-summary--checkout-blocks/billing-ad--c5eb4dcd-frontend.js 19 kB
build/vendors--atomic-block-components/add-to-cart-frontend.js 6.82 kB
build/vendors--atomic-block-components/price--cart-blocks/line-items--cart-blocks/order-summary--checkout--8a3571de-frontend.js 5.71 kB
build/vendors--cart-blocks/line-items--checkout-blocks/order-summary-frontend.js 3.14 kB
build/vendors--cart-blocks/order-summary--checkout-blocks/billing-address--checkout-blocks/order-summary---eb4d2cec-frontend.js 4.75 kB
build/wc-blocks-editor-style-rtl.css 4.46 kB
build/wc-blocks-editor-style.css 4.46 kB
build/wc-blocks-google-analytics.js 1.56 kB
build/wc-blocks-middleware.js 949 B
build/wc-blocks-registry.js 2.7 kB
build/wc-blocks-shared-context.js 1.51 kB
build/wc-blocks-shared-hocs.js 1.14 kB
build/wc-blocks-style-rtl.css 21.6 kB
build/wc-blocks-style.css 21.6 kB
build/wc-blocks-vendors-style-rtl.css 1.28 kB
build/wc-blocks-vendors-style.css 1.28 kB
build/wc-blocks-vendors.js 65.5 kB
build/wc-blocks.js 2.96 kB
build/wc-payment-method-stripe.js 11.1 kB
build/wc-settings.js 2.61 kB

compressed-size-action

@senadir
Copy link
Member

senadir commented Jan 5, 2022

I actually wrote a reply for 5442 that got lost :/ the gist is that:

@mikejolley
Copy link
Member Author

@senadir just to be clear, are you saying we need to remove support for the filtering on the PHP side (that this PR fixes)?

@senadir
Copy link
Member

senadir commented Jan 11, 2022

I'm saying that this PR (along side availablePaymentMethods) is not needed.
Developers who don't want to adopt JS filtering can still continue to filter out using. PHP, the payment method would still be visible but the order cannot be placed.

@mikejolley
Copy link
Member Author

mikejolley commented Jan 11, 2022

@senadir

I'm afraid having two API (PHP and JS) doing the same thing might cause confusion and a subpar experience.

For us yes.

If we don't support woocommerce_available_payment_gateways in the client, developers who need to filter things out would need to do the same thing in PHP and JS, which might be tricky if the data used on the PHP side is not available in the client. Do you see any issues with this?

Where does this leave us with issue 5388?

@senadir
Copy link
Member

senadir commented Jan 11, 2022

If we don't support woocommerce_available_payment_gateways in the client, developers who need to filter things out would need to do the same thing in PHP and JS, which might be tricky if the data used on the PHP side is not available in the client. Do you see any issues with this?

I also see the issue of filtering using data that's only available in the frontend, which might not be a big issue since the data will get synced back to the client.

You raise a good point, which is probably a good time for us to reconsider our filtering approach, assuming we go with this, you now can filter payments using:

  • canMakePayment JS filter.
  • woocommerce_available_payment_gateways PHP filter.
  • payment_requirements PHP filter.

Which is a bit excessive and at least 1 method too much.

@ralucaStan how do you feel about the canMakePayment, how does it fit within this system, and if we now expose woocommerce_available_payment_gateways on the frontend, can we deprecate the canMakePayment filter?

@mikejolley the main thing that's preventing me from going with availablePaymentMethods is that we're overpopulating the cart response again, with checkout related data.

Where does this leave us with issue 5388?

Assuming we continue with not exposing and filtering available payment methods on the frontend, we can just close that with a response that we use frontend filters.

@ralucaStan
Copy link
Contributor

ralucaStan commented Jan 12, 2022

@ralucaStan how do you feel about the canMakePayment, how does it fit within this system, and if we now expose woocommerce_available_payment_gateways on the frontend, can we deprecate the canMakePayment filter?

canMakePayment is actually the place where we call the callbacks that extensions might register with registerPaymentMethodExtensionCallbacks to filter payment methods, so removing it is not really an option.

What do you thing that instead of adding a new availablePaymentMethods field in the response we add a isAvailable field per payment gateways. This would reflect the fact that a PHP filter was used to remove that specific payment gateway.

If this availability is something we want to take into consideration we could also do a general check in the canMakePayment function just by checking that paymentGateways.isAvailable === true, instead in doing the check per payment gateway (like here).

@mikejolley mikejolley closed this Mar 9, 2022
@nielslange nielslange deleted the experiment/available-methods-5388 branch August 18, 2023 09:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: checkout Issues related to the checkout block. focus: rest api Work impacting REST api routes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants