过滤变量产品订单Woocommerce

时间:2017-11-12 11:47:33

标签: php sql wordpress woocommerce orders

我不熟悉SQL或wp_query,我需要一些帮助。如果我有可变产品,如何通过一个或一组产品ID过滤现有订单?例如,如果我想过滤包含以下产品的订单,我该怎么办:蓝色衬衫(#12),黑色衬衫(#15)或红色衬衫(#16)?

我偶然发现了这段代码:

function retrieve_orders_ids_from_a_product_id( $product_id )
{
global $wpdb;

$table_posts = $wpdb->prefix . "posts";
$table_items = $wpdb->prefix . "woocommerce_order_items";
$table_itemmeta = $wpdb->prefix . "woocommerce_order_itemmeta";

// Define HERE the orders status to include in  <==  <==  <==  <==  <==  <==  <==
$orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";

# Requesting All defined statuses Orders IDs for a defined product ID
$orders_ids = $wpdb->get_col( "
    SELECT DISTINCT $table_items.order_id
    FROM $table_itemmeta, $table_items, $table_posts
    WHERE  $table_items.order_item_id = $table_itemmeta.order_item_id
    AND $table_items.order_id = $table_posts.ID
    AND $table_posts.post_status IN ( $orders_statuses )
    AND $table_itemmeta.meta_key LIKE '_product_id'
    AND $table_itemmeta.meta_value LIKE '$product_id'
    ORDER BY $table_items.order_item_id DESC"
);
// return an array of Orders IDs for the given product ID
return $orders_ids;
}

这似乎有我需要的要点,但我不知道在哪里放置此代码,如果我想对订单进行排序并让我自己(作为管理员)可见,但没有其他,或如何修改此以便检索产品ID数组的订单。将代码放入我的子主题中的functions.php会导致错误消息。

2 个答案:

答案 0 :(得分:0)

您可以使用get_posts()获取所有订单并循环浏览订单商品,以查找商品ID是否与$product_id变量匹配。如果匹配,在循环内部将结果添加到数组中。

function retrieve_orders_ids_from_a_product_id( $product_id ){

    $orders = get_posts( array(
        'numberposts' => -1,
        'post_type'   => wc_get_order_types(),
        'post_status' => array_keys( wc_get_order_statuses() ),
    ) );
    $orders_ids = array();

    foreach($orders as $o){
        $order_id = $o->ID;
        $order = new WC_Order($order_id);
        foreach( $order->get_items() as $item_key => $item_values ){
            $p_id = $item_values->get_product_id();
            if($p_id == $product_id){
                array_push($orders_ids, $order_id);
            }
        }
    }
    return $orders_ids;
}

答案 1 :(得分:0)

更新 - 获取产品ID数组的订单ID的SQL查询将是:

function get_orders_ids_from_product_ids( $product_ids )
{
    global $wpdb;

    $product_ids = implode( ',', $product_ids ); // Converting to string

    // HERE the orders status to include
    $orders_statuses = "'wc-completed', 'wc-processing', 'wc-on-hold'";

    # Requesting All defined statuses Orders IDs for a defined product ID
    $orders_ids = $wpdb->get_col( "
        SELECT DISTINCT woi.order_id
        FROM {$wpdb->prefix}woocommerce_order_items as woi
        INNER JOIN {$wpdb->prefix}posts AS p ON woi.order_id = p.ID
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        WHERE p.post_status IN ( $orders_statuses )
        AND woim.meta_key LIKE '_product_id'
        AND woim.meta_value IN ($product_ids)
        ORDER BY woi.order_item_id DESC
    ");

    // Return an array of Orders IDs for the given Product IDs
    return $orders_ids;
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

用法:

您可以将生成的Orders ID数组以这种方式传递给变量(在任何其他代码中)

    // HERE  your product IDs in the array
    $product_ids = array( 12, 15, 16 );

    // Passing the resulting array to a variable
    $Orders_ids = get_orders_ids_from_product_ids( $product_ids );

您可以获得所需变量产品的变量以及一系列订单ID ...这适用于所有类型的产品,包括变量产品的变体。