📌  相关文章
📜  如果订单在数组中有产品 id - PHP 代码示例

📅  最后修改于: 2022-03-11 14:53:56.905000             🧑  作者: Mango

代码示例1
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();
}