在管理面板中获取woocommerce中的商品数量

时间:2016-01-29 06:28:36

标签: wordpress woocommerce

我正在使用ni invoice插件,它会在您选择日期范围时显示所有订单。我还想要一个订单数量的字段。但我无法获得购买的产品数量。生成表的代码如下:

                <?php
            foreach($order_data as $k => $v){
                $order_total += isset($v->order_total)?$v->order_total:0;
            ?>
                <tr>
                    <td> <?php echo $v->order_id;?> </td>
                    <td> <?php echo $v->order_date;?> </td>
                    <td> <?php echo $v->***?> </td>

                    <td> <?php echo ucfirst ( str_replace("wc-","", $v->order_status));?> </td>



                </tr>   
            <?php }?>

在第3个td>标记中,我想显示购买的产品数量。我该如何获取?
$order_data变量如下$order_data = $this->get_order_data();get_order_data()函数如下:

function get_order_data()
    {
        $order_query = $this->get_order_query("DEFAULT");


        if(count($order_query)> 0){
            foreach($order_query as $k => $v){

                /*Order Data*/
                $order_id =$v->order_id;
                $order_detail = $this->get_order_detail($order_id);
                foreach($order_detail as $dkey => $dvalue)
                {
                        $order_query[$k]->$dkey =$dvalue;

                }
            }
        }
        else
        {
            echo "No Record Found";
        }
        return $order_query;
    }


get_order_detail函数是这样的:

function get_order_detail($order_id)
    {
        $order_detail   = get_post_meta($order_id);
        $order_detail_array = array();
        foreach($order_detail as $k => $v)
        {
            $k =substr($k,1);
            $order_detail_array[$k] =$v[0];
        }
        return  $order_detail_array;
    }

2 个答案:

答案 0 :(得分:1)

在你的插件文件名中,ni-order-list.php&#39;用你的文件代码替换这段代码。它工作正常。

[![<?php 
if ( ! defined( 'ABSPATH' ) ) { exit;}
include_once('ni-function.php'); 
if( !class_exists( 'ni_order_list' ) ) {
    class ni_order_list extends ni_function{
        public function __construct(){

        }
        public function page_init(){
        ?>
        <form id="ni_frm_order_export" class="ni-frm-order-export" name="ni_frm_order_export" action="" method="post">
            <table>
                <tr>
                    <td>Select Order</td>
                    <td><select name="select_order" id="select_order">
                    <option value="today">Today</option>
                    <option value="yesterday">Yesterday</option>
                    <option value="last_7_days">Last 7 days</option>    
                    <option value="last_30_days">Last 30 days</option>
                    <option value="this_year">This year</option>
                    </select>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:right"><input type="submit" value="Search" id="SearchOrder" /></td>
                </tr>
            </table>
            <input type="hidden" name="action" value="ni_action" />
            <input type="hidden" name="ni_action_ajax" value="ni_woocommerce_invoice" />
        </form>
        <div class="ajax_content"></div>
        <?php

        }
        function get_order_query($type="DEFAULT"){
            global $wpdb;   
            $today = date("Y-m-d");
            $select_order = $this->get_request("select_order");

            $query = "SELECT 
                posts.ID as order_id
                ,post_status as order_status
                , date_format( posts.post_date, '%Y-%m-%d') as order_date 
                FROM {$wpdb->prefix}posts as posts          
                WHERE 
                        posts.post_type ='shop_order' 
                        ";
                 switch ($select_order) {
                    case "today":
                        $query .= " AND   date_format( posts.post_date, '%Y-%m-%d') BETWEEN '{$today}' AND '{$today}'";
                        break;
                    case "yesterday":
                        $query .= " AND  date_format( posts.post_date, '%Y-%m-%d') = date_format( DATE_SUB(CURDATE(), INTERVAL 1 DAY), '%Y-%m-%d')";
                        break;
                    case "last_7_days":
                        $query .= " AND  date_format( posts.post_date, '%Y-%m-%d') BETWEEN date_format(DATE_SUB(CURDATE(), INTERVAL 7 DAY), '%Y-%m-%d') AND   '{$today}' ";
                        break;
                    case "last_30_days":
                            $query .= " AND  date_format( posts.post_date, '%Y-%m-%d') BETWEEN date_format(DATE_SUB(CURDATE(), INTERVAL 30 DAY), '%Y-%m-%d') AND   '{$today}' ";
                        break;  
                    case "this_year":
                        $query .= " AND  YEAR(date_format( posts.post_date, '%Y-%m-%d')) = YEAR(date_format(CURDATE(), '%Y-%m-%d'))";           
                        break;      
                    default:
                        $query .= " AND   date_format( posts.post_date, '%Y-%m-%d') BETWEEN '{$today}' AND '{$today}'";
                }
            $query .= "order by posts.post_date DESC";  

         if ($type=="ARRAY_A") /*Export*/
            $results = $wpdb->get_results( $query, ARRAY_A );
         if($type=="DEFAULT") /*default*/
            $results = $wpdb->get_results( $query); 
         if($type=="COUNT") /*Count only*/  
            $results = $wpdb->get_var($query);      
            //echo $query;
            echo mysql_error();
        //  $this->print_data($results);
            return $results;    
        }
        /*get_order_list*/
        function get_order_list()
        {
            $this->get_order_grid();    


        }
        function get_order_data()
        {
            $order_query = $this->get_order_query("DEFAULT");


            if(count($order_query)> 0){
                foreach($order_query as $k => $v){

                    /*Order Data*/
                    $order_id =$v->order_id;
                    $order_detail = $this->get_order_detail($order_id);
                    foreach($order_detail as $dkey => $dvalue)
                    {
                            $order_query\[$k\]->$dkey =$dvalue;

                    }
                }
            }
            else
            {
                echo "No Record Found";
            }
            return $order_query;
        }
        function get_order_grid()
        {
            $order_total = 0;
            $order_data = $this->get_order_data();

            //$this->print_data ($order_data);

            if(count($order_data)> 0)
            {
                ?>
               <div class="data-table">
                <table>
                    <tr>
                        <th>#ID</th>
                        <th>Order Date</th>
                        <th>Billing First Name</th> 
                        <th>Billing Email</th> 
                        <th>Billing Country</th> 
                        <th>Status</th>
                        <th>Order Qty</th>
                        <th>Order Currency</th>
                        <th>Order Total</th>
                        <th>Invoice</th>
                    </tr>

                <?php
                foreach($order_data as $k => $v){
                    $order_total += isset($v->order_total)?$v->order_total:0;

                    $order = new WC_Order( $v->order_id );
                    $itemdetails=$order->get_items();
                    $itemmeta_temp=array_column($itemdetails, 'item_meta');
                    //print_r(array_column($itemmeta_temp, '_qty')\[0\]\[0\]);
                    //print_r($itemdetails);


                ?>

                    <tr>
                        <td> <?php echo $v->order_id;?> </td>
                        <td> <?php echo $v->order_date;?> </td>
                        <td> <?php echo $v->billing_first_name;?> </td>
                        <td> <?php echo $v->billing_email;?> </td>
                        <td> <?php echo $this->get_country_name($v->billing_country);?> </td>
                        <td> <?php echo ucfirst ( str_replace("wc-","", $v->order_status));?> </td>
                        <td> <?php echo array_column($itemmeta_temp, '_qty')\[0\]\[0\];?> </td>
                        <td> <?php echo $v->order_currency;?> </td>
                        <td style="text-align:right"> <?php echo woocommerce_price($v->order_total);?> </td>

                        <td> <a href="<?php echo admin_url("admin.php?page=ni-woocommerce-invoice")."&ni-order-invoice=". $v->order_id  ; ?>">Invoice</a></td>
                    </tr>   
                <?php }?>
                </table>
                <div style="text-align:right; margin-top:10px">
                    <?php  echo woocommerce_price($order_total); ?>
                </div>
                <?php

                //$this->print_data(    $order_data );
            }
        }
        /*Get Order Header information*/
        function get_order_detail($order_id)
        {
            $order_detail   = get_post_meta($order_id);
            $order_detail_array = array();
            foreach($order_detail as $k => $v)
            {
                $k =substr($k,1);
                $order_detail_array\[$k\] =$v\[0\];
            }
            return  $order_detail_array;
        }
    }
}
?>][1]][1]

答案 1 :(得分:1)

您需要在139行($order_total += isset($v->order_total)?$v->order_total:0;

之后添加以下行
 $order = new WC_Order( $v->order_id );
 $itemdetails=$order->get_items();
 $itemmeta_temp=array_column($itemdetails, 'item_meta');
//print_r(array_column($itemmeta_temp, '_qty')[0][0]);

为您的字段添加HTML。