1

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..

0

You would add the meta_query portion to your wc_get_orders() as the only filter ...

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(
        'page'     => $current_page,
        'paginate' => true,

        'meta_query' => array(
            array(
                'key' => 'mycustom_id',
                'value' => get_current_user_id(),
                'compare' => '='
            )
        )

    ) ) );

    wc_get_template(
        'myaccount/orders.php',
        array(
            'current_page'    => absint( $current_page ),
            'customer_orders' => $customer_orders,
            'has_orders'      => 0 < $customer_orders->total,
        )
    );
}

This basically says get all the orders where order meta_key = 'mycustom_id' and meta_value = $current_user_id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.