Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
80 stars 29 forks source link

Only update token meta after updating token default if the meta key is found #581

Closed james-allan closed 3 months ago

james-allan commented 3 months ago

Fixes https://github.com/woocommerce/woocommerce-subscriptions/issues/4631

Description

If you update your default payment method on the My Account → Payment methods screen and the Payment method doesn't integrate with the woocommerce_subscription_payment_meta filter (eg WooPayments), then additional meta will be added to the subscription without a key.

Screenshot 2024-03-13 at 4 08 57 pm
Notice the final row of the has an empty meta key

This was caused by the foreach loop I've changed in this PR. If the loop didn't find a meta key used for tokens, it would just set it to an empty key because that's the default.

This PR fixes that.

How to test this PR

  1. Install WooPayments
  2. Install Woo Subscriptions
  3. Create 2 subscription products with different billing frequencies (weekly and monthly).
  4. Purchase the products together using a standard card 4242424242424242.
  5. Make another purchase using a different card eg 4000056655665556.
  6. Go to My Account → Payment methods
  7. Change the 4000056655665556 payment method to your default.

Screenshot 2024-03-13 at 4 15 33 pm

  1. In the notice that appears on the next page, click "Yes".
    • On trunk, check the order meta table for your subscription and you should notice there is an empty meta key with the token ID stored.
    • On this branch, make another subscription purchase using a new card, repeat steps 6-8 no empty additional meta key should be stored.

Zip of this branch for convenience: woocommerce-subscriptions-core.zip

trunk This branch
Screenshot 2024-03-22 at 1 18 19 pm Screenshot 2024-03-22 at 1 16 13 pm

Product impact

james-allan commented 3 months ago

I've also just double checked that using a gateway like Stripe which does use a meta key, it still works to update the token stored on subscriptions.