error_log() PHP function is not working in the context of WooCommerce > Status > Scheduled Actions

80 Views Asked by At

Subscription action reference https://woocommerce.com/document/subscriptions/develop/action-reference/ shows woocommerce_subscription_payment_complete should be running on both new orders and renewal orders.

However, I see nothing in my server logs (e.g. 'RUNNING woocommerce_subscription_payment_complete') for RENEWAL ORDERS, I only see the error_log entries for NEW orders.

UPDATE: I did some testing by creating a custom_log function to post messages to a custom file every time I report something to the main error_log() error.log file. It turns out that woocommerce_subscription_payment_complete IS running on BOTH new orders and renewal orders, as documented.

So the issue is that the error_log() PHP function is not working in the context of WooCommerce > Status > Scheduled Actions. Any idea why and how to fix it??

Here is my code:

add_action( 'woocommerce_subscription_payment_complete', function( $subscription ) {
    error_log('RUNNING woocommerce_subscription_payment_complete');
    custom_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);
    // Set the user to customer role
    $user->set_role( 'customer' );
    error_log('WOO-SUBSCRIPTION-PAYMENT-COMPLETE...ROLE SET TO CUSTOMER: ' . $user_id);
    custom_log('WOO-SUBSCRIPTION-PAYMENT-COMPLETE...ROLE SET TO CUSTOMER: ' . $user_id);
    
    //mark order status as completed
    $current_order = $subscription->get_last_order( 'all', 'any' );
    $current_order->update_status( 'completed' );
    
}, 100);
0

There are 0 best solutions below