Reputation:
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 the customer
should stay. I just need to add this other current_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..
Upvotes: 0
Views: 2046
Reputation: 3562
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:
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 the view_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 );
Upvotes: 1