• Resolved wag1998

    (@wag1998)


    Hi, I am trying to filter the orders and product counts so that they show only the counts for the current logged in authors. I have managed to get the orders and product lists in admin to display only to the current author but cant get the order counts to adjust.

    The UL above the orders/products is the one with the class of “subsubsub” I cant even find the template file where this is located.

    The code I’ve tried is this:

    function adjust_order_counts_before_shop_loop( $counts ) {
    // Get the current user.
    $user = wp_get_current_user();

    // Check if the current user is an admin.
    if ( $user->has_cap( ‘activate_plugins’ ) ) {
    return $counts;
    }

    // Get the current author ID.
    $current_author_id = $user->ID;

    // Loop through the counts and adjust them if the current user is not an admin.
    foreach ( $counts as $key => $count ) {
    if ( $count[‘author’] !== $current_author_id ) {
    unset( $counts[ $key ] );
    }
    }

    return $counts;
    }

    add_action( ‘woocommerce_before_shop_loop’, ‘adjust_order_counts_before_shop_loop’ );

    I also tried this hook:

    add_filter( ‘woocommerce_order_counts’, ‘woocommerce_order_count_by_author’ );

    Any help would be appreciated ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @wag1998

    Your approach is correct, but the hooks you’re using might not be the right ones.

    The woocommerce_before_shop_loop action is used to add custom content before the shop loop starts, and the woocommerce_order_counts filter doesn’t exist in the WooCommerce core. Therefore, these hooks might not help in this case.

    Instead, you could use the woocommerce_reports_get_order_report_data_args filter. This filter allows you to modify the arguments used in the SQL query that fetches the orders data for reports. You can use it to add an additional condition that checks for the order’s author.

    Please note that, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any further questions on development or custom coding, don’t hesitate to reach out to some of our great resources available for support. Our WooCommerce community is brimming with skilled open-source developers who are active on the following channels:

    I hope this information helps! If you need further assistance, please feel free to ask.

    Thanks!

    Thread Starter wag1998

    (@wag1998)

    Thank you

    I will try that.

    Am I posting in the right place here to ask questions about coding? Is this a community forum or only Woocommerce support?

    I have another thing I’m trying to achieve.

    I’m trying to have email alerts send only to the authors of product orders and one admin address to receive all alerts. I’ve been wrestling with Chat GPT for about a week now but nothing it gives me works.

    This is what it gave me that looks like it should work but it just stops any emails being sent. Any help would be appreciated, Thanks:

    add_filter('woocommerce_email_recipient_new_order', 'custom_modify_order_recipients', 10, 2);
    add_filter('woocommerce_email_recipient_cancelled_order', 'custom_modify_order_recipients', 10, 2);
    add_filter('woocommerce_email_recipient_failed_order', 'custom_modify_order_recipients', 10, 2);
    function custom_modify_new_order_recipients($recipient, $order) {
    // Get the order author's ID
    $order_author_id = $order->get_user_id(); // Get the current user's ID
    $current_user_id = get_current_user_id(); // Define the admin email address that should always receive the email
    $admin_email = '[email protected]'; // Replace with the admin email address

    // Check if the current email is the admin email
    if ($recipient === $admin_email) {
    return $recipient; // Return the email as is
    } // Only send the email to the order's author
    if ($order_author_id === $current_user_id) {
    return $recipient;
    } else {
    // Return an empty string to prevent sending the email
    return '';
    }
    }

    Hi @wag1998 ,

    I’d be happy to clarify!

    While customization is outside our scope of support here, this is a good place to ask customization questions as there may be someone within the community that can help you out with this.

    This being said, we’ll leave the thread open for a bit to see if anyone can chime in with a solution for you.

    On the other hand, feel free to check out the resources shared by @shameemreza above :).

    Hope this helps!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce order counts’ is closed to new replies.