1

I want to display the order table found in woocommerce my-account recent orders for a specific product id on a separate page. To be more precise, display all recent orders for current user if the order has a product with the product_id of X.

I am trying to use these references:

Woocommerce - How to show Order details (my-account) on a separate page

Get all Orders IDs from a product ID in Woocommerce

Woocommerce: Get all orders for a product

But I am not able to relate $product_id to $user_id = get_current_user_id(); so that all the orders of the current user for a particular product id can be displayed on a separate page.

Edit: added code. I tried this code but its not working.

function orders_from_product_id( $product_id ) {

        $order_statuses = array('wc-on-hold', 'wc-processing', 'wc-completed');

        $customer_user_id = get_current_user_id(); // current user ID here for example

        $customer_orders = wc_get_orders( array(
            'meta_key' => '_customer_user',
            'meta_value' => $customer_user_id,
            'post_status' => $order_statuses,
            'numberposts' => -1
        ) );

        foreach($customer_orders as $order ){

            $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

            foreach($order->get_items() as $item_id => $item){

                $product_id = method_exists( $item, 'get_product_id' ) ? $item->get_product_id() : $item['product_id'];

                    if ( $product_id == 326 ) {
                        ob_start();
                    wc_get_template( 'myaccount/my-orders.php', array(
                        'current_user'  => get_user_by( 'id', $user_id),
                        'order_count'   => $order_count
                     ) );
                    return ob_get_clean();
                    } else {
                    echo '<p> You donot have any orders.</p>';
                    }
            } 
        }
}

add_shortcode('woocommerce_orders', 'orders_from_product_id');
depar
  • 49
  • 1
  • 7
  • to confirm you want.... All recent orders for current user if the order has a product with the product_id of X? – mikerojas Sep 15 '20 at 00:34
  • @mikerojas that's correct. I have tried few lines of code, do you want me to add them – depar Sep 15 '20 at 00:49

1 Answers1

1

From this code you to get the Current user-product id which product purchased by the user.

// GET CURR USER
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) return;

// GET USER ORDERS (COMPLETED + PROCESSING)
$customer_orders = get_posts( array(
    'numberposts' => -1,
    'meta_key'    => '_customer_user',
    'meta_value'  => $current_user->ID,
    'post_type'   => wc_get_order_types(),
    'post_status' => array_keys( wc_get_is_paid_statuses() ),
) );
// LOOP THROUGH ORDERS AND GET PRODUCT IDS
if ( ! $customer_orders ) return;
$product_ids = array();
foreach ( $customer_orders as $customer_order ) {
    $order = wc_get_order( $customer_order->ID );
    $items = $order->get_items();
    foreach ( $items as $item ) {
        $product_id = $item->get_product_id();
        $product_ids[] = $product_id;
    }
}
$product_ids = array_unique( $product_ids );