Automattic / woocommerce-subscriptions-core

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

error_log not working on renewal orders in woocommerce_subscription_payment_complete hook #534

Closed rlmajcom closed 11 months ago

rlmajcom commented 11 months ago

Describe the bug

error_log() function calls are not working on renewal orders in woocommerce_subscription_payment_complete hook. For new order, error log gets populated, for renewal orders, it does not.

To Reproduce

  1. Add a error_log call such as add_action( 'woocommerce_subscription_payment_complete', function( $subscription ) { error_log('RUNNING woocommerce_subscription_payment_complete');
  2. Set up a new subscription and/or run a renewal

Expected behavior

error.log reporting should work as expected in both new orders and renewals, and you will see the output of error_log() appear in your normal system error.log file such as /www/username/logs/error.log

Actual behavior

Product impact

Additional context

james-allan commented 11 months ago

hey @rlmajcom

I haven't been able to replicate this following the steps you've outlined.

When purchasing a new subscription or renewing an existing one, that hook and code snippet you gave did run and did correctly write to the error log file -- wp-content/debug.log in my case.

I'm going to close this one as we cannot replicate.

rlmajcom commented 11 months ago

Hi @james-allan Can you confirm if the error_log() reported correctly during an AUTOMATIC renewal (e.g. one ran via scheduled action)?

My exact code that does NOT report to the error_log() in the case of woocommerce_subscription_payment_complete during automatic renewals is below. The code runs in both new sales and renewals, and it does report to the error_log() for new sales. Also, the hook woocommerce_order_status_changed reports to the error_log for both new sales and renewals. Any idea what is incorrect in my code below if this is not a bug of some sort?


add_action( 'woocommerce_subscription_payment_complete', function( $subscription ) {
    error_log('RUNNING woocommerce_subscription_payment_complete');

    // Getting the user ID from the current subscription object
    $user_id = $subscription->get_user_id();

    // create a WP User object given the user_id
    $user = new WP_User($user_id);
    $user->set_role( 'customer' );
    error_log('WOO-SUBSCRIPTION-PAYMENT-COMPLETE...ROLE SET TO CUSTOMER: ' . $user_id);
    // automatically set the latest order to completed (otherwise is stays in processing)
    $current_order = $subscription->get_last_order( 'all', 'any' );
    $current_order->update_status( 'completed' );

}, 100);
add_action( 'woocommerce_order_status_changed', function ( $order_id, $old_status, $new_status, $order ) {
    if ( $old_status === 'pending' && ( $new_status === 'cancelled' || $new_status === 'failed' ) ) {
        $user_id =  $order->get_user_id();
        $user =  new WP_User( $user_id );
        // revert to pending customer so the user will be redirected off of map tools
        // only revert to pending customer if the user does NOT have another active subscription
       if ( !wcs_user_has_subscription( $user_id, '', 'active' ) && !wcs_user_has_subscription( $user_id, '', 'pending-cancel' ) ) {
            $user->set_role( '_pending_customer' );
            error_log('USER MOVED FROM CUSTOMER TO PENDING CUSTOMER DUE TO LAGGING PAYMENT REJECTION: ' . $user_id );
           }
    }
}, 100, 4);