0

if user buy my woocommerce products (id= 1900,1902,1938). Then I want to put some other condition into it. Can anyone help me with that please . i have tried this Check if a customer has purchased a specific products in WooCommerce

But it didn't work for me as it works just for one product (but not for many products).

Here is my code:

function has_bought_items() {
    $bought = false;

    // Set HERE ine the array your specific target product IDs
    $prod_arr = array( '1900','1902','1938');

    // Get all customer orders
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => 'shop_order', // WC orders post type
        'post_status' => 'wc-completed' // Only orders with status "completed"
    ) );
    foreach ( $customer_orders as $customer_order ) {
        // Updated compatibility with WooCommerce 3+
        $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
        $order = wc_get_order( $customer_order );

        // Iterating through each current customer products bought in the order
        foreach ($order->get_items() as $item) {
            // WC 3+ compatibility
            if ( version_compare( WC_VERSION, '3.0', '<' ) ) 
                $product_id = $item['product_id'];
            else
                $product_id = $item->get_product_id();

            // Your condition related to your 2 specific products Ids
            if ( in_array( $product_id, $prod_arr ) ) 
                $bought = true;
        }
    }
    // return "true" if one the specifics products have been bought before by customer
    return $bought;
}

Then I use it this way in an IF / ELSE statements:

if ( has_bought_items() ) { 
    // my condtion
} else { 
    //no condition
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Please explain what _exactly_ _“this work for just one product . not many product”_ means. – CBroe Jun 05 '20 at 08:59
  • if user just buy one product (id 1900) then he can see the condition . . but i want if user buy 3 product (id 1900,1902,1938) then he will see the condition ..i hope you will understand .. and thanks for comment – Shantø Rk Jun 05 '20 at 10:56
  • So you want to do something only if they bought all three of these products? Well then you can not just set the flag to true as soon as you encounter one of them, you need to _count_ how many of the products you are looking for you actually find in the orders. – CBroe Jun 05 '20 at 11:04
  • yes .. thanks .. i have tried to remove true flag . and doing what you said . but i get error . can you provide me code ... please – Shantø Rk Jun 05 '20 at 11:21
  • Show what you tried, and give a proper problem description (that would include _quoting_ any error messages you get, instead of just saying “get error”.) – CBroe Jun 05 '20 at 11:38
  • i removed $bought = false; and if ( in_array( $product_id, $prod_arr ) ) $bought = true; this line ..and now condition is removed for one product .. not get any error . and i am not good coder ..so i can do wrong . – Shantø Rk Jun 05 '20 at 12:13
  • Initialize `$bought` with 0 before the loops, and inside the `in_array` condition check, increase it by 1. Then, after the loops, check if that counter now matches the count of ids you had in your `$prod_arr` - return true or false accordingly. – CBroe Jun 05 '20 at 12:19
  • You didn't tried [this improved same function](https://stackoverflow.com/a/46217461/3730754) that handle multiple product IDs. It wtill perfectly work on last woocommerce version. – LoicTheAztec Jun 05 '20 at 12:49

0 Answers0