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

Fatal Error on Adding Coupon with Recurring Product to Cart #630

Open
felipepitol opened this issue May 29, 2024 · 1 comment
Open

Fatal Error on Adding Coupon with Recurring Product to Cart #630

felipepitol opened this issue May 29, 2024 · 1 comment
Labels
type: bug The issue is a confirmed bug.

Comments

@felipepitol
Copy link

Describe the bug

I encountered a fatal error after activating the Smart Coupons plugin and generating a coupon that offers a discount on a recurring product, limited to 3 payments. The error occurred when attempting to add the product and apply the coupon directly to the cart using the following URL structure:

/checkout/?add-to-cart=26470&quantity=1&coupon_code=DISCOUNT24

Fatal error: Uncaught Error: Call to a member function supports() on string in .../wp-content/plugins/woocommerce-subscriptions/includes/class-wcs-limited-recurring-coupon-manager.php on line 166

To Reproduce

  1. Activate the Smart Coupons plugin.
  2. Create a coupon with a discount on a recurring product, limited to 3 payments.
  3. Use the URL to add the product and coupon directly to the cart (e.g., /checkout/?add-to-cart=26470&quantity=1&coupon_code=DISCOUNT24). // you need to change to your product id

Detailed Error Explanation:

The error is caused by the function limit_gateways_subscription_amount_changes() in the class-wcs-limited-recurring-coupon-manager.php file. This function attempts to call the supports() method on $gateway objects. However, under certain conditions, $gateway may be a string instead of an object, leading to a fatal error.

Solution:

To fix this issue, I added a check to ensure that $gateway is an object before calling the supports() method. Here is the updated code for the limit_gateways_subscription_amount_changes() function:

 * Limits payment gateways to those that support changing subscription amounts.
 *
 * @since 4.0.0
 * @param WC_Payment_Gateway[] $gateways The current available gateways.
 * @return WC_Payment_Gateway[]
 */
private static function limit_gateways_subscription_amount_changes( $gateways ) {
	foreach ( $gateways as $index => $gateway ) {
		if ( is_object( $gateway ) && $gateway->supports( 'subscriptions' ) && ! $gateway->supports( 'subscription_amount_changes' ) ) {
			unset( $gateways[ $index ] );
		}
	}

	return $gateways;
}
  • [ x ] Does this issue affect WooCommerce Subscriptions? yes/no/tbc, add issue ref
  • [ x ] Does this issue affect WooCommerce Payments? yes/no/tbc, add issue ref

Summary:

This fix ensures that the supports() method is only called on valid objects, preventing the fatal error from occurring. Please consider incorporating this fix into the plugin to avoid similar issues for other users.

@felipepitol felipepitol added the type: bug The issue is a confirmed bug. label May 29, 2024
@felipepitol
Copy link
Author

felipepitol commented Aug 17, 2024

I encountered a fatal error when applying a coupon to a recurring product using WooCommerce Subscriptions 6.5.0. The issue arises due to the limit_gateways_subscription_amount_changes function in the class-wcs-limited-recurring-coupon-manager.php file. This function attempts to call the supports() method on $gateway objects, but under certain conditions, $gateway can be a string, leading to a fatal error.

To temporarily resolve this issue without modifying the core plugin files, you can add the following code snippet to your theme's (or child theme's) functions.php file:

function custom_limit_gateways_subscription_amount_changes( $gateways ) {
    foreach ( $gateways as $index => $gateway ) {
        if ( is_object( $gateway ) && $gateway->supports( 'subscriptions' ) && ! $gateway->supports( 'subscription_amount_changes' ) ) {
            unset( $gateways[ $index ] );
        }
    }

    return $gateways;
}

// Remove the original filter
remove_filter( 'woocommerce_available_payment_gateways', array( 'WCS_Limited_Recurring_Coupon_Manager', 'gateways_subscription_amount_changes' ), 20 );

// Add the custom filter
add_filter( 'woocommerce_available_payment_gateways', 'custom_limit_gateways_subscription_amount_changes', 20 );

This code snippet checks if $gateway is an object before calling the supports() method, preventing the fatal error from occurring. While this is a temporary fix, it ensures your site continues to function correctly until an official update addresses the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug The issue is a confirmed bug.
Projects
None yet
Development

No branches or pull requests

1 participant