Search
 
SCRIPT & CODE EXAMPLE
 

PHP

if order has product id in array

function get_order_ids_from_bought_items( $product_ids = 0, $customer_id = 0 ) {
    global $wpdb;

    $customer_id = $customer_id == 0 || $customer_id == '' ? get_current_user_id() : $customer_id;
    $statuses    = array_map( 'esc_sql', wc_get_is_paid_statuses() );

    if ( is_array( $product_ids ) )
        $product_ids = implode(',', $product_ids);

    if ( $product_ids !=  ( 0 || '' ) )
        $meta_query_line = "AND woim.meta_value IN ($product_ids)";
    else
        $meta_query_line = "AND woim.meta_value != 0";

    // Get Orders IDs
    $results = $wpdb->get_col( "
        SELECT DISTINCT p.ID FROM {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
        AND pm.meta_key = '_customer_user'
        AND pm.meta_value = $customer_id
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        $meta_query_line
    " );

    // Return an array of Order IDs or an empty array
    return sizeof($results) > 0 ? $results : array();
}
Comment

PREVIOUS NEXT
Code Example
Php :: https://stackoverflow.com/questions/34545641/php-artisan-makeauth-command-is-not-defined 
Php :: Limit number of words to be displayed on blog post excerpt with Laravel 
Php :: check if product has crosssell woocommerce 
Php :: "^" means in php 
Php :: laravel 8 remove public folder from url 
Php :: install php 7.4 amazon linux 2 
Php :: get git branch with php 
Php :: PHP utf8_decode — Converts a string from UTF-8 to ISO-8859-1, replacing invalid or unrepresentable characters 
Php :: how to set tinyint default 0 laravel migration 
Php :: wordpress add_action echo on edit page 
Php :: auto complete order paid3 
Php :: implement class in autoloader athow to implment data table in laravel project 
Php :: How to hide tax details from woocommerce order emails 
Php :: php import 
Php :: xampp php 
Php :: Agregar clases de rol al body en WordPress 
Php :: Convert Shamsi Jalali Persian Date TimeStamp 
Php :: send emails with runtime configurations in laravelk 
Php :: connecting to database and performing sql queries 
Php :: Automatically downloading images from any URL location 
Php :: array issue in php 
Php :: Obtener rol de usuario registrado en WordPress 
Php :: Online Food Ordering System Project SQL 
Php :: launch new tab and refresh original page codeigniter 
Php :: phpmaker check master details page 
Php :: yii2 label of ActiveField 
Php :: woocommerce subscriptions custom user rolde 
Php :: php adding options from an array 
Php :: wp-query 
Php :: php make simple loop of number elevated to exponent without pow 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =