Woocommerce循环中每个订单商品的总数

时间:2018-07-12 21:23:01

标签: php sql wordpress woocommerce orders

嗨,我正在同时学习PHP和Woocommerce! 我正在尝试获取所有具有状态处理的订单以及每个订单的总数,并将其显示在页面上。

到目前为止,我可以遍历所有订单并获取每个订单的名称和数量。

但是由于我不知道列表中会列出哪种产品,所以我不确定如何比较名称然后添加数量。

我当前的输出是这样的:

  • prod1-v1 x 1

  • Prod2-v3 x 1

  • prod2-v3 x 1

  • prod3-v2 x 11

  • prod3-v2 x 1

我想要的是:

  • prod1-v1 x 1

  • Prod2-v3 x 2

  • prod3-v2 x 12

当前代码为:

    <?php
/*
Template Name: Print Supplier Order
*/
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.');
?>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title><?php _e('Processing Orders'); ?></title>
    <style>
        body { background:white; color:black; width: 95%; margin: 0 auto; }
    </style>
</head>
<body>
    <header>
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

            <h1 class="title"><?php the_title(); ?></h1>

            <?php the_content(); ?>

        <?php endwhile; endif; ?>
    </header>
    <section>
    <?php 

    global $woocommerce;

$args = array( 'post_type' => 'shop_order', 'post_status' => 'wc-processing', 'posts_per_page' => -1 );


    $loop = new WP_Query( $args );

    while ( $loop->have_posts() ) : $loop->the_post();

        $order_id = $loop->post->ID;

        $order = new WC_Order($order_id);

        $product_details = array();
        $order_items = $order->get_items();
        foreach( $order_items as $product ) {
                echo $product['name']." x ".$product['qty'];
                echo '<br>';

            }
        ?>
    <?php endwhile; ?>
    </section>
</body>
</html>

2 个答案:

答案 0 :(得分:5)

  

已更新 (现在,SQL查询还提供了产品名称,使其更轻巧)

与其使用WP_Query和一些繁琐的代码来进行计算,不如使用WPDB Class(SQL查询):

global $wpdb;

$results = $wpdb->get_results( "
    SELECT DISTINCT woim2.meta_value as id, SUM(woim.meta_value) as count, woi.order_item_name as name
    FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim
    INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON woi.order_item_id = woim.order_item_id
    INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim2 ON woi.order_item_id = woim2.order_item_id
    INNER JOIN {$wpdb->prefix}posts as p ON p.ID = woi.order_id
    WHERE p.post_status IN ('wc-processing','wc-on-hold')
    AND woim.meta_key LIKE '_qty'
    AND woim2.meta_key LIKE '_product_id'
    GROUP BY woim2.meta_value
" );

foreach( $results as $result ){
    echo $result->name . ' (' . $result->id . ') x ' . $result->count . '<br>';
}

产品计数将仅基于处理可能性状态,而不基于产品总销售额。

经过测试,可以正常工作。


取而代之的是产品变体 (如您的评论所述)

根据您的评论中的要求获取订单中的产品款式,您将替换以下行:

 AND woim2.meta_key LIKE '_product_id'

通过以下行:

 AND woim2.meta_key LIKE '_variation_id'

获取所有产品和版本(不包括可变产品)

要获取包括产品变体但不包括可变产品在内的所有产品,请使用:

global $wpdb;

$results = $wpdb->get_results( "
    SELECT DISTINCT woim2.meta_value as id, SUM(woim.meta_value) as count, woi.order_item_name as name
    FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim
    INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON woi.order_item_id = woim.order_item_id
    INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim2 ON woi.order_item_id = woim2.order_item_id
    INNER JOIN {$wpdb->prefix}posts as p ON p.ID = woi.order_id
    WHERE p.post_status IN ('wc-processing','wc-on-hold') 
    AND woim.meta_key LIKE '_qty'
    AND ((woim2.meta_key LIKE '_variation_id' AND woim2.meta_value > 0)
    OR (woim2.meta_key LIKE '_product_id'
    AND woim2.meta_value NOT IN (SELECT DISTINCT post_parent FROM {$wpdb->prefix}posts WHERE post_type LIKE 'product_variation')))
    GROUP BY woim2.meta_value
" );

foreach( $results as $result ){
    echo $result->name . ' (' . $result->id . ') x ' . $result->count . '<br>';
}

经过测试,可以正常工作。


要全部使用它们 (甚至包括产品变体和可变产品)

global $wpdb;

$results = $wpdb->get_results( "
    SELECT DISTINCT woim2.meta_value as id, SUM(woim.meta_value) as count, woi.order_item_name as name
    FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim
    INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON woi.order_item_id = woim.order_item_id
    INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim2 ON woi.order_item_id = woim2.order_item_id
    INNER JOIN {$wpdb->prefix}posts as p ON p.ID = woi.order_id
    WHERE p.post_status IN ('wc-processing','wc-on-hold')
    AND woim.meta_key LIKE '_qty'
    AND ((woim2.meta_key LIKE '_variation_id' AND woim2.meta_value > 0)
    OR woim2.meta_key LIKE '_product_id' )
    GROUP BY woim2.meta_value
" );

foreach( $results as $result ){
    echo $result->name . ' (' . $result->id . ') x ' . $result->count . '<br>';
}

经过测试,可以正常工作。

答案 1 :(得分:2)

方法1

您可以通过几种不同的方法来解决此问题,但是如果我们只想稍微调整代码,则可以通过使用产品名称作为键将产品循环中的产品添加到自定义数组中来实现此目的,然后遍历该数组。

这是一个例子:

global $woocommerce;

//custom array to list products:
$products_array = array();

//start wp query
$args = array( 
    'post_type'     => 'shop_order', 
    'post_status'   => 'wc-processing', 
    'posts_per_page'=> -1 
);
$loop = new WP_Query( $args );

//loop through orders
while ( $loop->have_posts() ) {
    $loop->the_post();

    //vars
    $order_id           = $loop->post->ID;
    $order              = new WC_Order($order_id);
    $product_details    = array();
    $order_items        = $order->get_items();

    //loop through products
    foreach( $order_items as $product ) {
        //if product has been captured to custom array before, increment value, else add it.
        if (array_key_exists($product['name'], $products_array)) {
            $products_array[$product['name']] += $product['qty'];
        } else {
            $products_array[$product['name']] = $product['qty'];
        }

    }
}

//end wp_query
wp_reset_postdata();

//display list:
foreach ($products_array as $title => $quantity) {
    echo $title.' x '.$quantity;
}

方法2

遍历所有产品,而不是所有订单(过了一会儿,假设商店成功了,那么订单可能比产品多得多,因此从长远来看,这种方法可能会更快)。每个产品都将其总销售额存储为元值,因此访问它非常简单:

//WP Query args
$args = array(
    'post_type'     => 'product',
    'posts_per_page'=> -1,
    'meta_key'      => 'total_sales',
    'orderby'       => 'title',
    'order'         => 'ASC',
    'meta_query'    => array( //make sure we only grab items that have actually sold
        array(
            'key'       => 'total_sales',
            'value'     => 0,
            'compare'   => '>'
        )
    )
);
$query = new WP_Query($args);

//loop through products
if($query->have_posts()) {
    while($query->have_posts()) {
        $query->the_post();
        //use built in Wordpress functions for get_the_title() and get_post_meta()
        echo get_the_title().' x '.get_post_meta(get_the_ID(), 'total_sales', true);
    }
}

//end wp query
wp_reset_postdata();