2017-04-25 39 views
1

我正在嘗試編寫PHP代碼,以表格形式按降序列出最暢銷商品(含數量)和暢銷商品類別(含總銷售額)。WooCommerce清單最暢銷產品和類別

項目|數量

類別|銷售

我知道我們可以從WooCommerce的Reports屏幕中檢索這個值,但我需要獲取這個值,以便我可以在widget的WordPress管理面板中顯示它們。順便說一句,我知道如何創建儀表板小部件。

有誰知道如何實現這個?任何幫助表示讚賞。

謝謝。

回答

1

您應該查看woocommerce文件並查找WC_Admin_Report。你可以看到一些例子,並以你想要的方式工作。

我給你舉個例子。的print_r($data);

Array 
(
    [0] => stdClass Object 
     (
      [quantity] => 4 
      [gross] => 140 
      [product_id] => 53 
      [order_item_name] => Happy Ninja 
     ) 

    [1] => stdClass Object 
     (
      [quantity] => 3 
      [gross] => 36 
      [product_id] => 70 
      [order_item_name] => Flying Ninja 
     ) 

    [2] => stdClass Object 
     (
      [quantity] => 1 
      [gross] => 10 
      [product_id] => 50 
      [order_item_name] => Patient Ninja 
     ) 

) 

add_action('init', 'reports'); 

function reports() { 


    include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php'); 
    $wc_report = new WC_Admin_Report(); 

    $data = $wc_report->get_order_report_data(array(
     'data' => array(
      '_qty' => array(
       'type' => 'order_item_meta', 
       'order_item_type' => 'line_item', 
       'function' => 'SUM', 
       'name' => 'quantity' 
      ), 
      '_line_subtotal' => array(
       'type' => 'order_item_meta', 
       'order_item_type' => 'line_item', 
       'function' => 'SUM', 
       'name' => 'gross' 
      ), 
      '_product_id' => array(
       'type' => 'order_item_meta', 
       'order_item_type' => 'line_item', 
       'function' => '', 
       'name' => 'product_id' 
      ), 
      'order_item_name' => array(
       'type'  => 'order_item', 
       'function' => '', 
       'name'  => 'order_item_name', 
      ), 
     ), 

     'group_by'  => 'product_id', 

     'order_by'  => 'quantity DESC', 

     'query_type' => 'get_results', 

     'limit' => 20, 

     'order_status' => array('completed', 'processing'), 
    )); 

    print_r($data); 

} 

樣本輸出你的數據和將讓你和運行Item | Quantity

+0

謝謝,你還可以展示我如何解析並以表格格式顯示?謝謝,真的很感激。 – Amjad

相關問題