Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
88 stars 33 forks source link

Uncaught TypeError: array_sum() in `get_cart_item_from_session()` when paying for a renewal order #504

Open mattallan opened 1 year ago

mattallan commented 1 year ago

Describe the bug

Customer report in ticket 6706536-zen:

2023-08-21T20:04:48+00:00 CRITICAL Uncaught TypeError: array_sum(): Argument [#1](https://a8c.zendesk.com/agent/tickets/1) ($array) must be of type array, null given in /var/www/html/wp-content/plugins/woocommerce-subscriptions/vendor/woocommerce/subscriptions-core/includes/class-wcs-cart-renewal.php:449
Stack trace:
[#0](https://a8c.zendesk.com/agent/tickets/0) /var/www/html/wp-content/plugins/woocommerce-subscriptions/vendor/woocommerce/subscriptions-core/includes/class-wcs-cart-renewal.php(449): array_sum()
[#1](https://a8c.zendesk.com/agent/tickets/1) /var/www/html/wp-includes/class-wp-hook.php(310): WCS_Cart_Renewal->get_cart_item_from_session()
[#2](https://a8c.zendesk.com/agent/tickets/2) /var/www/html/wp-includes/plugin.php(205): WP_Hook->apply_filters()
[#3](https://a8c.zendesk.com/agent/tickets/3) /var/www/html/wp-content/plugins/woocommerce/includes/class-wc-cart-session.php(196): apply_filters()
[#4](https://a8c.zendesk.com/agent/tickets/4) /var/www/html/wp-includes/class-wp-hook.php(310): WC_Cart_Session->get_cart_from_session()
[#5](https://a8c.zendesk.com/agent/tickets/5) /var/www/html/wp-includes/class-wp-hook.php(334): WP_Hook->apply_filters()
[#6](https://a8c.zendesk.com/agent/tickets/6) /var/www/html/wp-includes/plugin.php(517): WP_Hook->do_action()
[#7](https://a8c.zendesk.com/agent/tickets/7) /var/www/html/wp-settings.php(654): do_action()
[#8](https://a8c.zendesk.com/agent/tickets/8) /var/www/html/wp-config.php(155): require_once('...')
[#9](https://a8c.zendesk.com/agent/tickets/9) /var/www/html/wp-load.php(50): require_once('...')
[#10](https://a8c.zendesk.com/agent/tickets/10) /var/www/html/wp-admin/admin-ajax.php(22): require_once('...')
[#11](https://a8c.zendesk.com/agent/tickets/11) {main}
thrown w /var/www/html/wp-content/plugins/woocommerce-subscriptions/vendor/woocommerce/subscriptions-core/includes/class-wcs-cart-renewal.php w linii 449

The line that is causing this issue is this line:

$price += array_sum( $item_to_renew['taxes']['subtotal'] );

I've been able to reproduce this locally by creating a pending renewal, getting that order in your cart session, then removing the line item. Here are the steps to reproduce:

  1. Enable taxes and choose tax-inclusive prices
  2. Purchase a subscription product and creating a pending renewal order
  3. Go to your My Account page and click "Pay" for this pending renewal
  4. While on the checkout page, open a new tab and go to the Edit Order page for this renewal order
  5. Delete the order items
  6. Go back to the checkout page and you'll see the critical error on the page.

While debugging this issue I noticed, when we are setting $item_to_renew we are making an assumption that the order item exists.

To fix this I think we should add some defensive code in this function.

mattallan commented 1 year ago

Another report: 6704927-zen

mattallan commented 1 year ago

This issue might be resolved by: https://github.com/Automattic/woocommerce-subscriptions-core/pull/498

shevene commented 11 months ago

7162966-zen