Fatal Error on Adding Coupon with Recurring Product to Cart
Opened this issue · 1 comments
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
- Activate the Smart Coupons plugin.
- Create a coupon with a discount on a recurring product, limited to 3 payments.
- 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.
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.