Search code examples
woocommercehook-woocommerce

Is there a reason the "Emails" tab in WooCommerce Settings breaks after the use of custom code?


I have used the following custom code.

add_filter( 'woocommerce_email_recipient_new_order', 'custom_wc_email_recipient_new_order', 10, 2 );
function custom_wc_email_recipient_new_order( $recipient, $order ) {
 
    // Get the user ID of the order's creator
    $user_id = $order->get_user_id();

    // Get the user's role
    $user = get_userdata( $user_id );
    $user_role = $user->roles[0];
 
    // Only send the email to the admin email if the customer has the specified user role
    if ( $user_role == 'role1' ) {
        return $recipient .= ', admin@website.com';
    }
 
    // Return the original recipient for all other user roles
    return $recipient;
}

The code works perfectly fined and does what it is required to do, however, once the code has been used once (so once an order has been placed) if I try to access the "Emails" tab in "WooCommerce > Settings", I get a fatal error on the website and all I see is the below image.

WooCommerce Emails tab error

Is there a reason for this, and if so, a way I can fix it?

EDIT - Added part of the error log:

[20-Dec-2022 17:12:00 UTC] WordPress database error Unknown column 'wp_postmeta.post_id' in 'where clause' for query SELECT meta_id FROM wp_8hkq051x71_postmeta,
                (SELECT DISTINCT post_id FROM wp_8hkq051x71_postmeta
                WHERE (meta_key = '_billing_country' OR meta_key='_shipping_country') AND meta_value='UA')
                AS states_in_country
            WHERE (meta_key='_billing_state' OR meta_key='_shipping_state')
            AND meta_value='CV'
            AND wp_postmeta.post_id = states_in_country.post_id
            LIMIT 100 made by do_action_ref_array('action_scheduler_run_queue'), WP_Hook->do_action, WP_Hook->apply_filters, ActionScheduler_QueueRunner->run, ActionScheduler_QueueRunner->do_batch, ActionScheduler_Abstract_QueueRunner->process_action, ActionScheduler_Action->execute, do_action_ref_array('woocommerce_run_update_callback'), WP_Hook->do_action, WP_Hook->apply_filters, WC_Install::run_update_callback, wc_update_721_adjust_ukraine_states, Automattic\WooCommerce\Database\Migrations\MigrationHelper::migrate_country_states, Automattic\WooCommerce\Database\Migrations\MigrationHelper::migrate_country_states_for_orders

I have many lines like the one above, the only part that seems to change on each line is the:

AND meta_value='CV'

The CV changes to CH, CK, KS, etc.

Second Edit - Code Fix

The initial problem with the code was a WooCommerce bug. With that bug solved, I still couldn't modify email recipients in the WooCommerce Emails tab in the Settings. To be able to modify recipients there you need to use the following modified code.

add_filter( 'woocommerce_email_recipient_new_order', 'custom_wc_email_recipient_new_order', 10, 2 );
function custom_wc_email_recipient_new_order( $recipient, $order ) {
    
    if ( $order ) {
         // Get the user ID of the order's creator
        $user_id = $order->get_user_id();

        // Get the user's role
        $user = get_userdata( $user_id );
        $user_role = $user->roles[0];

        // Only send the email to the admin email if the customer has the specified user role
        if ( $user_role == 'role1' ) {
            return $recipient .= ', admin@website.com';
        }

        // Return the original recipient for all other user roles
        return $recipient;
    }
    return $recipient;
}

Solution

  • Thanks for posting the error log, now it all makes sense. It's a database error, it says wp_postmeta.post_id is missing. This happens because you are using a custom database prefix wp_8hkq051x71 and some code that generated this SQL wasn't using $wpdb->postmeta or $wpdb->prefix. Instead, there was a hardcoded wp_postmeta value and that table really doesn't exists in your database.

    But how that happened?

    WooCommerce team made some changes to country states, in this case Ukrainian states and they made a bug inside the update script. They already fixed it 19 hours ago: https://github.com/woocommerce/woocommerce/commit/6a1a7d7e15f488064f872020d42b7a58a2980c38

    So just update WooCommerce to the latest version and the bug will disappear.

    Also, I would highly recommend you to use only stable releases of WooCommerce instead of latest dev versions.

    Current stable version is 7.1.1 and current dev version with included fix for this issue is 7.2.1 (https://github.com/woocommerce/woocommerce/releases/tag/7.2.1)

    The problem is not related to your custom_wc_email_recipient_new_order at all. It's just a coincidence that you noticed this bug after you added your change.