php - WooCommerce: How can I get orders with a custom ID in order meta data object?


Because of a project I need help from you. I've searched a lot but I can't find a solution. I'm trying to edit a WooCommerce function named

  • woocommerce_account_orders

I've added the field

  • mycustom_id

to the the orders meta-data object because I need to get all orders which has the current logged in user in the field mycustom_id:

  • (mycustom_id = current_user_id())

The check for thecustomer should stay. I just need to add this othercurrent_user_id check.

This sould stay as it is:

  • 'customer' => get_current_user_id()

. This is my not working code snippet:

function woocommerce_account_orders( $current_page ) {
    $current_page    = empty( $current_page ) ? 1 : absint( $current_page );
    $customer_orders = wc_get_orders( apply_filters( 'woocommerce_my_account_my_orders_query', array(
        'customer' => get_current_user_id(),
        'mycustom_id' => get_current_user_id(),
        'page'     => $current_page,
        'paginate' => true,
    ) ) );

            'current_page'    => absint( $current_page ),
            'customer_orders' => $customer_orders,
            'has_orders'      => 0 < $customer_orders->total,

The method is located in:

How can I add this feature to the function a smart way like a filter and how can I pass my custom parameter the right way to the function? I've saved the parameter as an order_meta attribute:

[5] => WC_Meta_Data Object (
     [current_data:protected] => Array (
          [id] => 3477
          [key] => mycustom_id
          [value] => 2

Thank you for your help. I've tried so much but I'm new in PHP and must lurn a lot..




if you want to change this query you can do it by calling thewoocommerce_my_account_my_orders_query filter instead of modifying the core function so you can achieve the required desired as follow:

add_filter( 'woocommerce_my_account_my_orders_query', 'modify_my_order_query', 10, 1 );

function modify_my_order_query( $q ) {
    $q['customer'] = [ 1, 3 ]; // you customers ids here 

    return $q;

but this function above will display the order in the list only and if the customer click on that order to see the details he will got an error message as he doesn't have the permission to view the order and you need to modify theview_order capability

for example you can give the user with ID 1 permission to view user ID 3 orders as follow:

function give_permissions( $allcaps, $cap, $args ) {
    $user = 3; //user id 
    if ( $cap[0] == 'view_order' ) {
        $allcaps[ $cap[0] ] = true;
    return ( $allcaps );
add_filter( 'user_has_cap', 'give_permissions', 10, 3 );

People are also looking for solutions to the problem: php - Wrong table formatting for sqlserver grammar


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.