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,
) ) );
wc_get_template(
'myaccount/orders.php',
array(
'current_page' => absint( $current_page ),
'customer_orders' => $customer_orders,
'has_orders' => 0 < $customer_orders->total,
)
);
}
The method is located in: https://docs.woocommerce.com/wc-apidocs/source-function-woocommerce_account_orders.html#2465-2486
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..
Answer
Solution:
if you want to change this query you can do it by calling the
woocommerce_my_account_my_orders_query
filter instead of modifying the core function so you can achieve the required desired as follow: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 the
view_order
capabilityfor example you can give the user with ID 1 permission to view user ID 3 orders as follow: