2017-07-25 79 views

回答

0

更新:

不是簡單...,但我得到它。見下圖:

enter image description here

1)獲取數據,並計算:
這裏是有點複雜的SQL查詢的自定義功能,可用於獲取每個產品類別的所有相關產品的庫存數量。然後在foreach循環中,按產品類別添加庫存數量,以獲得按類別的總庫存評估。

/** 
* Get the Total Stock Valuation by Category. 
* 
* @param integer $category_id (optional) 
* @return an array with the category ID (int) as key and total stock quantity (int) as value 
*/ 
function get_product_cats_stock_qty($category_id = 0){ 
    global $wpdb; 


    // The DB tables involved 
    $term_tax = $wpdb->prefix . "term_taxonomy"; 
    $term_rel = $wpdb->prefix . "term_relationships"; 
    $postmeta = $wpdb->prefix . "postmeta"; 

    if(0 != $category_id) 
    { // get one defined category 
     $one_category_only = "AND $term_tax.term_id LIKE $category_id"; 
    } else 
    { // get all categories 
     $one_category_only = ''; 
    } 

    // Query all detailed categories and related product stock quantity 
    $results_obj = $wpdb->get_results(" 
     SELECT $term_rel.term_taxonomy_id as cat_id, 
     $term_rel.object_id as prod_id, $postmeta.meta_value as stock_qty 
     FROM $term_rel, $postmeta, $term_tax 
     WHERE $term_rel.object_id = $postmeta.post_id 
     AND $term_rel.term_taxonomy_id = $term_tax.term_id 
     $one_category_only 
     AND $term_tax.taxonomy LIKE 'product_cat' 
     AND $term_tax.count > 0 
     AND $postmeta.meta_key LIKE '_stock' 
     AND $postmeta.meta_value != '' 
     ORDER BY $term_rel.term_taxonomy_id ASC 
    "); 

    // Iterating though each categories/products relationships stock quantity 
    foreach($results_obj as $result){ 
     // Initialising each different category stock 
     if(empty($stock_qty_by_cat_id[ $result->cat_id ])) 
      $stock_qty_by_cat_id[ $result->cat_id ] = 0; 
     // category stock quantity calculation 
     $stock_qty_by_cat_id[ $result->cat_id ] += intval($result->stock_qty); 
    } 
    return $stock_qty_by_cat_id 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

2)ADMIN報告股票部分自定義標籤:

下面是將添加一個自定義選項卡與相應的自定義內容的功能:您將獲得的所有產品類別的清單,併爲他們每個人所有相關產品庫存數量總和。

// Add a Custom tab to admin repport stock section 
add_filter('woocommerce_admin_reports', 'add_my_custom_stock_admin_report', 10, 1); 
function add_my_custom_stock_admin_report($reports){ 
    $reports["stock"]["reports"]["category_stock_valuation"] = array(
     'title'  => __('Category Stock Valuation', 'woocommerce'), 
     'description' => '', 
     'hide_title' => true, 
     'callback' => 'content_custom_stock_admin_report', 
    ); 
    return $reports; 
} 

// The content for the Custom tab to admin repport stock section 
function content_custom_stock_admin_report(){ 

    ?> 
    <div id="poststuff" class="woocommerce-reports-wide"> 
     <table class="wp-list-table widefat fixed striped stock"> 
      <thead> 
       <tr> 
        <th scope="col" id="cat_id" class="manage-column column-category-id column-primary"><?php _e('Term ID','woocommerce'); ?></th> 
        <th scope="col" id="cat_name" class="manage-column column-category-name"><?php _e('Category Name','woocommerce'); ?></th> 
        <th scope="col" id="stock_level" class="manage-column column-stock_level"><?php _e('Stock count','woocommerce'); ?></th> 
       </tr> 
      </thead> 
      <tbody id="the-list" data-wp-lists="list:stock"> 
       <?php 
        foreach(get_product_cats_stock_qty() as $cat_id => $cats_stock_qty ): 
         $cat_name = $cats_stock_qty['cat_name']; // category name 
         $stock_qty = $cats_stock_qty['stock_qty']; // Stock quantity 
       ?> 
       <tr> 
        <td class="category-id column-category-id column-primary" data-colname="Product"><?php echo $cat_id; ?></td> 
        <td class="category-name column-category-name" data-colname="Parent"><?php echo $cat_name; ?></td> 
        <td class="stock_level column-stock_level" data-colname="Units in stock"><?php echo $stock_qty; ?></td> 
       </tr> 
       <?php endforeach; ?> 
      </tbody> 
      <tfoot> 
       <tr> 
        <th scope="col" id="cat_id" class="manage-column column-category-id column-primary"><?php _e('Term ID','woocommerce'); ?></th> 
        <th scope="col" id="cat_name" class="manage-column column-category-name"><?php _e('Category Name','woocommerce'); ?></th> 
        <th scope="col" id="stock_level" class="manage-column column-stock_level"><?php _e('Stock count','woocommerce'); ?></th> 
       </tr> 
      </tfoot> 
     </table> 
    </div> 
    <?php 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

此代碼在WooCommerce 3+上測試並正常工作。