Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
87 stars 32 forks source link

[Customisation] remove_item() creates PHP Fatal error: Uncaught Error #270

Open rouxsean opened 2 years ago

rouxsean commented 2 years ago

Describe the bug

Customisation calling $subscription->remove_item($item_id); may trigger a fatal error. PHP Fatal error: Uncaught Error: Call to a member function meta_exists() on bool in /wp-content/plugins/woocommerce-subscriptions/vendor/woocommerce/subscriptions-core/includes/admin/class-wcs-admin-meta-boxes.php:587

How to reproduce

Sample customisation from functions.php, hooking woocommerce_subscription_status_updated:

function delete_items_on_subscription_hold($subscription, $new_status, $old_status)
{
    $items = $subscription->get_items();
    if (count($items) > 0 && $new_status == 'on-hold') {
        foreach ($items as $item_id => $item) {
            $subscription->remove_item($item_id);
            $subscription->add_order_note('Old item removed');
            $subscription->calculate_totals();
            $subscription->add_order_note('Totals calculated after removal');
        }
    }
}
add_action('woocommerce_subscription_status_updated', 'delete_items_on_subscription_hold', 5, 3);

With the customisation above, change a subscription from Active to On hold in admin. This triggers the fatal error.

More details

Stack trace:

#0 /public_html/wp-includes/class-wp-hook.php(308): WCS_Admin_Meta_Boxes::update_subtracted_base_location_taxes_amount(6171, Array)

#1 /public_html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters(NULL, Array)

#2 /public_html/wp-includes/plugin.php(517): WP_Hook->do_action(Array)

#3 /public_html/wp-content/plugins/woocommerce/includes/admin/wc-admin-functions.php(290): do_action('woocommerce_bef...', 6171, Array)

#4 /public_html/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-items.php(53): wc_save_order_items(6171, Array)

#5 /public_html/ in /public_html/wp-content/plugins/woocommerce-subscriptions/vendor/woocommerce/subscriptions-core/includes/admin/class-wcs-admin-meta-boxes.php on line 587

class-wcs-admin-meta-boxes.php on line 587:

                        // If this item's subtracted tax data hasn't been repaired, do that now.
            if ( $line_item->**meta_exists**( '_subtracted_base_location_tax' ) ) {
                WC_Subscriptions_Upgrader::repair_subtracted_base_taxes( $line_item->get_id() );
                $line_item = WC_Order_Factory::get_order_item( $line_item->get_id() );
            }

            if ( ! $line_item->meta_exists( '_subtracted_base_location_taxes' ) ) {
                continue;
            }

To Reproduce

programatically delete an item from a subscription object

syntax: $subscription->remove_item($item_id);

Product impact

WooCommerce Subscriptions

WooCommerce status report:

``` ### WordPress Environment ### WordPress address (URL): https://app-631cb571c1ac188968e8ebb6.closte.com Site address (URL): https://app-631cb571c1ac188968e8ebb6.closte.com WC Version: 7.1.0 REST API Version: ✔ 7.1.0 WC Blocks Version: ✔ 8.7.5 Action Scheduler Version: ✔ 3.4.0 Log Directory Writable: ✔ WP Version: 6.1.1 WP Multisite: – WP Memory Limit: 512 MB WP Debug Mode: – WP Cron: ✔ Language: en_US External object cache: ✔ ### Server Environment ### Server Info: LiteSpeed PHP Version: 7.4.26 PHP Post Max Size: 135 MB PHP Time Limit: 60 PHP Max Input Vars: 10000 cURL Version: 7.71.0 OpenSSL/1.1.1d SUHOSIN Installed: – MySQL Version: 5.5.5-10.3.28-MariaDB-cll-lve Max Upload Size: 128 MB Default Timezone is UTC: ✔ fsockopen/cURL: ✔ SoapClient: ✔ DOMDocument: ✔ GZip: ✔ Multibyte String: ✔ Remote Post: ✔ Remote Get: ✔ ### Database ### WC Database Version: 7.1.0 WC Database Prefix: wp_ Total Database Size: 5.00MB Database Data Size: 3.42MB Database Index Size: 1.58MB wp_woocommerce_sessions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_woocommerce_api_keys: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_attribute_taxonomies: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_woocommerce_downloadable_product_permissions: Data: 0.02MB + Index: 0.06MB + Engine InnoDB wp_woocommerce_order_items: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_woocommerce_order_itemmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_tax_rates: Data: 0.02MB + Index: 0.06MB + Engine InnoDB wp_woocommerce_tax_rate_locations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_shipping_zones: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_woocommerce_shipping_zone_locations: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_shipping_zone_methods: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_woocommerce_payment_tokens: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_woocommerce_payment_tokenmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_woocommerce_log: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_actionscheduler_actions: Data: 0.02MB + Index: 0.11MB + Engine InnoDB wp_actionscheduler_claims: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_actionscheduler_groups: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_actionscheduler_logs: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_commentmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_comments: Data: 0.02MB + Index: 0.09MB + Engine InnoDB wp_links: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_litespeed_url: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_litespeed_url_file: Data: 0.02MB + Index: 0.08MB + Engine InnoDB wp_options: Data: 2.48MB + Index: 0.06MB + Engine InnoDB wp_postmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_posts: Data: 0.02MB + Index: 0.06MB + Engine InnoDB wp_termmeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_terms: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_term_relationships: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_term_taxonomy: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_usermeta: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_users: Data: 0.02MB + Index: 0.05MB + Engine InnoDB wp_wc_admin_notes: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_wc_admin_note_actions: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_wc_category_lookup: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_wc_customer_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_wc_download_log: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_wc_order_coupon_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_wc_order_product_lookup: Data: 0.02MB + Index: 0.06MB + Engine InnoDB wp_wc_order_stats: Data: 0.02MB + Index: 0.05MB + Engine InnoDB wp_wc_order_tax_lookup: Data: 0.02MB + Index: 0.03MB + Engine InnoDB wp_wc_product_attributes_lookup: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_wc_product_download_directories: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_wc_product_meta_lookup: Data: 0.02MB + Index: 0.09MB + Engine InnoDB wp_wc_rate_limits: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_wc_reserved_stock: Data: 0.02MB + Index: 0.00MB + Engine InnoDB wp_wc_tax_rate_classes: Data: 0.02MB + Index: 0.02MB + Engine InnoDB wp_wc_webhooks: Data: 0.02MB + Index: 0.02MB + Engine InnoDB ### Post Type Counts ### attachment: 1 page: 7 post: 2 product: 1 product_variation: 2 shop_subscription: 1 wp_global_styles: 1 ### Security ### Secure connection (HTTPS): ✔ Hide errors from visitors: ✔ ### Active Plugins (2) ### WooCommerce Subscriptions: by WooCommerce – 4.6.0 WooCommerce: by Automattic – 7.1.0 ### Inactive Plugins (1) ### LiteSpeed Cache: by LiteSpeed Technologies – 5.3 ### Dropin Plugins (1) ### object-cache.php: object-cache.php ### Must Use Plugins (1) ### closte-requirements.php: by – ### Settings ### API Enabled: – Force SSL: – Currency: USD ($) Currency Position: left Thousand Separator: , Decimal Separator: . Number of Decimals: 2 Taxonomies: Product Types: external (external) grouped (grouped) simple (simple) subscription (subscription) variable (variable) variable subscription (variable-subscription) Taxonomies: Product Visibility: exclude-from-catalog (exclude-from-catalog) exclude-from-search (exclude-from-search) featured (featured) outofstock (outofstock) rated-1 (rated-1) rated-2 (rated-2) rated-3 (rated-3) rated-4 (rated-4) rated-5 (rated-5) Connected to WooCommerce.com: – Enforce Approved Product Download Directories: ✔ ### WC Pages ### Shop base: #6 - /?page_id=6 Cart: #7 - /?page_id=7 Checkout: #8 - /?page_id=8 My account: #9 - /?page_id=9 Terms and conditions: ❌ Page not set ### Theme ### Name: Hello Elementor Version: 2.6.1 Author URL: https://elementor.com/?utm_source=wp-themes&utm_campaign=author-uri&utm_medium=wp-dash Child Theme: ❌ – If you are modifying WooCommerce on a parent theme that you did not build personally we recommend using a child theme. See: How to create a child theme WooCommerce Support: ✔ ### Templates ### Overrides: – ### Subscriptions ### WCS_DEBUG: ✔ No Subscriptions Mode: ✔ Live Subscriptions Live URL: https://app-631cb571c1ac188968e8ebb6.closte.com Subscription Statuses: wc-on-hold: 1 WooCommerce Account Connected: ❌ No Report Cache Enabled: ✔ Yes Cache Update Failures: ✔ 0 failure ### Store Setup ### Country / State: United States (US) — Pennsylvania ### Payment Gateway Support ### Check payments: products ### Admin ### Enabled Features: activity-panels analytics coupons customer-effort-score-tracks experimental-products-task experimental-import-products-task experimental-fashion-sample-products shipping-smart-defaults shipping-setting-tour homescreen marketing multichannel-marketing mobile-app-banner navigation onboarding onboarding-tasks remote-inbox-notifications remote-free-extensions payment-gateway-suggestions shipping-label-banner subscriptions store-alerts transient-notices woo-mobile-welcome wc-pay-promotion wc-pay-welcome-page Disabled Features: minified-js new-product-management-experience settings Daily Cron: ✔ Next scheduled: 2022-11-17 21:10:08 +00:00 Options: ✔ Notes: 36 Onboarding: completed ### Action Scheduler ### Canceled: 6 Oldest: 2022-11-16 21:31:51 +0000 Newest: -0001-11-30 00:00:00 +0000 Complete: 19 Oldest: 2022-11-16 21:16:44 +0000 Newest: 2022-11-16 21:32:52 +0000 Pending: 6 Oldest: 2022-11-16 21:37:31 +0000 Newest: 2022-11-17 21:11:13 +0000 ### Status report information ### Generated at: 2022-11-16 21:33:50 +00:00 ```
haszari commented 2 years ago

@rouxsean could you give us some more details about your setup to help us debug this? A WooCommerce status report is helpful.

In particular, please confirm you can reproduce this bug with minimum extensions active, e.g. WooCommerce, WooCommerce Subscriptions and a default theme (e.g. Storefront or Twenty Twenty Two). Thank you!

rouxsean commented 2 years ago

OK. I spun up a shiny new wordpress instance for you. Installed only woo & woo subscriptions along with Hello theme. (litespeed cache deactivated.

Here's the system report: [edited for brevity, system report is in description now]

haszari commented 2 years ago

Thank you for the system report @rouxsean !

I have one more question – how do you trigger the fatal error? If you can reproduce this reliably by using the WooCommerce UI please add steps to the description. Include config details that might be relevant (e.g. tax settings, subscription product config, subscription / order details).

From the call stack, it looks like it's something to do with viewing order items metabox. What are you doing in the admin UI when the bug occurs?

Thanks again 😄

haszari commented 2 years ago

@Jinksi can you try reproducing this? If it's a fatal error that's easily reproducible we should prioritise a fix.

rouxsean commented 2 years ago

Here's the code I'm using.

On this new spin-up test site, it's in the functions.php file

function delete_items_on_subscription_hold($subscription, $new_status, $old_status) { $items = $subscription->get_items(); if ( count( $items ) > 0 && $new_status == 'on-hold') { foreach ( $items as $item_id => $item ) { $subscription->remove_item($item_id); $subscription->add_order_note('Old item removed'); $subscription->calculate_totals(); $subscription->add_order_note('Totals calculated after removal');

}}} add_action('woocommerce_subscription_status_updated', 'delete_items_on_subscription_hold', 5, 3);

haszari commented 2 years ago

Thanks for the info @rouxsean , good to confirm this is a customisation issue (and not affecting sites without custom code).

rouxsean commented 2 years ago

Sorry. I should have included the entire code.

It's this line that's causing the issue:

$subscription->remove_item($item_id);

The order notes are added, so my code runs all the way through, but a PHP Fatal error is thrown when woo subscriptions tries to see if _subtracted_base_location_tax exists through the meta_exists() function. (line 587 of class-wcs-admin-meta-boxes.php)

Since I'm on the admin screen when I change the status from Active to on-hold, I get the wordpress white screen of death.

rouxsean commented 2 years ago

Also, this error happens when I change the subscription from active to on-hold from the admin screen. This is how I've been testing this.

Screenshot 2022-11-16 at 5 12 46 PM

An alternative way to test this is to process a pending renewal order. This will put the subscription on-hold until the renewal order is marked as paid. When I test this way, there is no PHP Error.

Screenshot 2022-11-16 at 5 13 14 PM

As a result, this is probably a much lower priority that I thought (generated as a result of testing method).

Not sure if there's a way to reduce priority, but I'm going to continue to develop using the pending renewal as the way to change subscription status.

Thanks!

haszari commented 2 years ago

Thanks @rouxsean – I've tagged low priority 👍

Great you've found a workaround!