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

Applying coupon on subscription renewal throws PHP Fatal error: Uncaught Error: Call to a member function is_coupon_emails_allowed() on null in /wp-content/plugins/woocommerce/includes/class-wc-discounts.php:979 #47330

Closed
2 of 5 tasks
mslepko opened this issue May 9, 2024 · 4 comments · Fixed by #47589
Assignees
Labels
focus: coupon Issues related to coupons. priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. team: Rubik Store API checkout endpoints, Mini-Cart, Cart and Checkout related issues type: community contribution type: refactor The issue/PR is related to refactoring.

Comments

@mslepko
Copy link

mslepko commented May 9, 2024

Prerequisites

  • I have carried out troubleshooting steps and I believe I have found a bug.
  • I have searched for similar bugs in both open and closed issues and cannot find a duplicate.

Describe the bug

We are applying custom coupon codes on subscription renewals and due to the is_coupon_emails_allowed function introduced a couple of weeks ago we’ve started to get errors with subscription renewals.

2024-05-01T11:01:13+00:00 Notice scheduled action XYZ (subscription payment) failed to finish processing due to the following exception: Call to a member function is_coupon_emails_allowed() on null  
Additional context
{
    "_legacy": true
}

2024-05-01T11:01:13+00:00 Notice action args: subscription_id: XYZ

It looks like WC()->cart is null in this scenario in https://github.com/woocommerce/woocommerce/blob/release/8.8.0.20/plugins/woocommerce/includes/class-wc-discounts.php#L979

[29-Apr-2024 09:51:25 UTC] PHP Fatal error:  Uncaught Error: Call to a member function is_coupon_emails_allowed() on null in /wp-content/plugins/woocommerce/includes/class-wc-discounts.php:979
Stack trace:
#0 /wp-content/plugins/woocommerce/includes/class-wc-discounts.php(1046): WC_Discounts->validate_coupon_allowed_emails(Object(WC_Coupon))
#1 /wp-content/plugins/woocommerce/includes/class-wc-discounts.php(252): WC_Discounts->is_coupon_valid(Object(WC_Coupon))
#2 /wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-order.php(1252): WC_Discounts->apply_coupon(Object(WC_Coupon))
#3 /wp-content/themes/my_theme/custom/discount-on-renewal.php(132): WC_Abstract_Order->apply_coupon('renewal_couponXYZ')

My custom function is called using this filter

add_filter( 'wcs_renewal_order_created', 'apply_discount_on_renewal_order_created', 10, 2 );

function apply_discount_on_renewal_order_created( $renewal_order, $subscription ) {
    $coupon_codes = array( 'renewal_code1', 'renewal_code2', 'renewal_code3');

    $order = new WC_Order( $renewal_order->get_id() );
 
   foreach ( $coupon_codes as $coupon_code ) {
	  $coupon        = new WC_Coupon( $coupon_code ); 
	  $expiry_date = $coupon->get_date_expires();

	  if( $coupon->get_usage_count() < $coupon->get_usage_limit() && (date('Y-m-d') < date('Y-m-d', strtotime($expiry_date)) ) ){
		$order->apply_coupon($coupon_code);
	   }
     }
}

I’ve already fixed the issue in my theme and wrapped the code in try/catch block but I’m wondering if this shouldn’t be fixed in WooCommerce code as well as cart is not available it this specific scenarion.

Thanks
Michal

Expected behavior

Email restricted coupons should be applied on subscription renewals

Actual behavior

Fatal error is thrown

Steps to reproduce

Apply an email restricted coupon code on subscription renewals

WordPress Environment

`

WordPress Environment

WordPress address (URL): http://prodog.local
Site address (URL): http://prodog.local
WC Version: 8.8.3
REST API Version: ✔ 8.8.3
Action Scheduler Version: ✔ 3.7.4
Log Directory Writable: ✔
WP Version: ❌ 6.4.1 - There is a newer version of WordPress available (6.5.3)
WP Multisite: –
WP Memory Limit: 1 GB
WP Debug Mode: –
WP Cron: –
Language: en_GB
External object cache: –

Server Environment

Server Info: Apache/2.4.43 (Unix)
PHP Version: 8.2.10
PHP Post Max Size: 1,000 MB
PHP Time Limit: 1200
PHP Max Input Vars: 4000
cURL Version: 8.4.0
(SecureTransport) LibreSSL/3.3.6

SUHOSIN Installed: –
MySQL Version: 5.7.28
Max Upload Size: 300 MB
Default Timezone is UTC: ✔
fsockopen/cURL: ✔
SoapClient: ✔
DOMDocument: ✔
GZip: ✔
Multibyte String: ✔
Remote Post: ❌ wp_remote_post() failed. Contact your hosting provider.
Remote Get: ❌ wp_remote_get() failed. Contact your hosting provider.

Database

WC Database Version: 7.8.2
WC Database Prefix: wp_
Total Database Size: 10896.83MB
Database Data Size: 5296.99MB
Database Index Size: 5599.84MB

Status report information

Generated at: 2024-05-09 16:01:47 +01:00
`

Isolating the problem

  • I have deactivated other plugins and confirmed this bug occurs when only WooCommerce plugin is active.
  • This bug happens with a default WordPress theme active, or Storefront.
  • I can reproduce this bug consistently using the steps above.
@adimoldovan adimoldovan added focus: coupon Issues related to coupons. team: Proton labels May 10, 2024
@barryhughes barryhughes added team: Rubik Store API checkout endpoints, Mini-Cart, Cart and Checkout related issues and removed team: Proton labels May 17, 2024
@barryhughes
Copy link
Member

Changing team, as this seems to have its origin in this recent change (for which Proton doesn't have the same amount of context):

#43872

@wavvves wavvves self-assigned this May 17, 2024
@wavvves wavvves added the type: refactor The issue/PR is related to refactoring. label May 17, 2024
@wavvves
Copy link
Contributor

wavvves commented May 17, 2024

Added type:refactor as this is a current code limitation.

@mslepko Thank you for reporting this, we've identified some improvements in our code to ensure this specific use case is viable.

@ralucaStan ralucaStan added the priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. label May 23, 2024
@wavvves
Copy link
Contributor

wavvves commented May 23, 2024

Hi again @mslepko, a fix is underway to be on the next WooCommerce release. In the meantime, and since there's no current ETA for that release, you can try instantiating the Cart object so that the method doesn't fail, so right before applying the coupon on the order add:

WC()->cart = new WC_Cart();
$order->apply_coupon($coupon_code);

This will be compatible with the planned fix, but you can remove that line after updating.

@mslepko
Copy link
Author

mslepko commented May 23, 2024

thank you @wavvves

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
focus: coupon Issues related to coupons. priority: high The issue/PR is high priority—it affects lots of customers substantially, but not critically. team: Rubik Store API checkout endpoints, Mini-Cart, Cart and Checkout related issues type: community contribution type: refactor The issue/PR is related to refactoring.
Projects
None yet
5 participants