在过去的一年中,客户购买了多少次产品

时间:2016-10-27 18:20:31

标签: wordpress woocommerce

在woocommerce中,如果过去一年的产品价格为0,如何检查客户购买产品的次数。为了达到这个目的,我尝试下面的代码并单独得到答案,结果不是产品名称或ID的缩写。请帮忙如何缩短它们。

以下是代码

//Check if bought it in last one year and the number of quantity
function customer_bought_qty(){

    //get the value of targeted product
    $_options = get_option( 'license_page_option_name' );
    $ex_product_ids = $_options['ex_product_ids_warranty']; 
    $target_products = explode(",",$ex_product_ids); //array(18,63,85)


    // 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"
        'date_query' => array(
                array(
                    'column' => 'post_date_gmt',
                    'after' => '1 year ago',
                )
            )
        ) );

   // Going through each orders for current customer 
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order );
        $order_id = $order->id;
        $items = $order->get_items();
        $subtotal = $order->get_subtotal(); //to check how many free item bought

    //Loop trough the order items and see if it is a product we need to cehck
        foreach($items as $item) {          
            if($subtotal== 0 && in_array($item['product_id'], $target_products)) {  
                $_product_id = $item['product_id'];
                $_product_qty = $item['qty'];
                if(isset ($_product_id)){
                    echo '('. get_the_title($_product_id). ')# You bought '.$_product_qty.' Times<br>';
                }
            }   
        }
    }

}

输出看起来

(Product 2)# You bought 1 Times
(Product 1)# You bought 1 Times
(Product 1)# You bought 1 Times
(Product 2)# You bought 1 Times
(Product 1)# You bought 1 Times
(Product 2)# You bought 1 Times
(Product 1)# You bought 1 Times
(Product 2)# You bought 1 Times

但我需要的结果应该是

(Product 2)# You bought 4 Times
(Product 1)# You bought 4 Times

1 个答案:

答案 0 :(得分:0)

要完成您需要一个新数组来存储每个产品的实际数量

<?php
        $query="SELECT * FROM movies WHERE id_movie=(select max(id_movie) from movies)";
        $result = mysqli_query($link, $query) or die("erreurrequete");
        if (mysqli_num_rows($result)>0) {
            while($row = mysqli_fetch_assoc($result)){
                printf("<div class='col-md-4 text-center'>
                    <img class='img-thumbnail' src='%s'>
                    <h3>%s</h3>
                    <h3>%s</h3>
                    </div>", $row['img_movie'], $row['title'], $row['boxoffice']);
            }
            mysqli_free_result($result);
        }
        ?>

当您 foreach 项目时,您更新此阵列

$quantity_per_product = array();

因此您的实际代码应如下所示:

if(array_key_exists($_product_id, $quantity_per_product)){
   $quantity_per_product[$_product_id] += $item['qty']
}
else{
   $quantity_per_product[$_product_id] = $item['qty']
}

希望这有帮助!