0

After buying a product add this product order ID in this user meta. I can check the product bought status using this wc_customer_bought_product(). Now I need to get this product order id using UserID and product ID. How can I achieve this? My ultimate goal is after getting order id I will remove order by this function wp_delete_post()

$bronze = wc_customer_bought_product($current_user->user_email, $current_user->ID, 246014);

function get_customerorderid(){
    global $post;
    $order_id = $post->ID;

    // Get an instance of the WC_Order object
    $order = wc_get_order($order_id);

    // Get the user ID from WC_Order methods
    $user_id = $order->get_user_id(); // or $order->get_customer_id();

    return $user_id;
}
get_customerorderid();
wp_delete_post(246014,true);
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Kane
  • 605
  • 2
  • 8
  • 22

1 Answers1

0

You can do that making a custom sql query embedded in a function, using WPDB Class, this way:

function get_completed_orders_for_user_from_product_id( $product_id, $user_id = 0 ) {
    global $wpdb;

    $order_status = 'wc-completed';

    // If optional $user_id argument is not set, we use the current user ID
    $customer_id = $user_id === 0 ? get_current_user_id() : $user_id;

    // Return customer orders IDs containing the defined product ID
    return $wpdb->get_col( $wpdb->prepare("
        SELECT DISTINCT woi.order_id
        FROM {$wpdb->prefix}posts p
        INNER JOIN {$wpdb->prefix}postmeta pm
            ON p.ID = pm.post_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_items woi
            ON p.ID = woi.order_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta woim
            ON woi.order_item_id = woi.order_item_id
        WHERE p.post_status = '%s'
        AND pm.meta_key = '_customer_user'
        AND pm.meta_value = '%d'
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        AND woim.meta_value LIKE '%d'
        ORDER BY woi.order_item_id DESC
    ", $order_status, $customer_id, $product_id ) );
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


USAGE with wp_delete_post() to remove related orders containing a specific product (id: 246014):

// Get all orders containing 246014 product ID for the current user 
$orders_ids = get_completed_orders_for_user_from_product_id( 246014 );

// Checking that, the orders IDs array is not empty
if( count($orders_ids) > 0 ) {

    // Loop through orders IDs
    foreach ( $orders_ids as $order_id ) {
        // Delete order post data 
        wp_delete_post( $order_id, true );
    }

    // Add the order(s) ID(s) in user meta (example)
    update_user_meta( get_current_user_id(), 'item_246014', implode( ',', $orders_ids ) );
}

Related threads:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • `get_completed_orders_from_customer_from_product_id()` and `get_completed_orders_for_user_from_product_id()` is not same. – Kane Sep 18 '19 at 04:16
  • @Kane Sorry, I have updated my code (a renaming mistake)… The function code works and the usage code should work too. – LoicTheAztec Sep 18 '19 at 05:26