php - Displaying Total Stock Valuation By Product Category In WooCoomerce -


i trying total stock valuation category in woocoommerce.

anyone have clue how can done?

i know it's simple code can't figure out.

enter image description here

thanks

updated:

not simple @ all… but it. see screenshot below:

enter image description here

1) data , calculate:
here custom function bit complex sql query each product category related products stock quantity. in foreach loop, add stock quantities product category total stock valuation category.

/**  * total stock valuation category.  *  * @param integer $category_id (optional)  * @return array category id (int) key , total stock quantity (int) value  */ function get_product_cats_stock_qty( $category_id = 0 ){     global $wpdb;       // db tables involved     $term_tax = $wpdb->prefix . "term_taxonomy";     $term_rel = $wpdb->prefix . "term_relationships";     $postmeta = $wpdb->prefix . "postmeta";      if( 0 != $category_id )     { // 1 defined category         $one_category_only = "and $term_tax.term_id $category_id";     } else      { // categories         $one_category_only = '';     }      // query detailed categories , related product stock quantity     $results_obj = $wpdb->get_results("         select  $term_rel.term_taxonomy_id cat_id,         $term_rel.object_id prod_id, $postmeta.meta_value stock_qty          $term_rel, $postmeta, $term_tax          $term_rel.object_id = $postmeta.post_id         , $term_rel.term_taxonomy_id = $term_tax.term_id         $one_category_only         , $term_tax.taxonomy 'product_cat'         , $term_tax.count > 0         , $postmeta.meta_key '_stock'         , $postmeta.meta_value != ''         order $term_rel.term_taxonomy_id asc     ");      // iterating though each categories / products relationships stock quantity     foreach($results_obj $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 } 

code goes in function.php file of active child theme (or theme) or in plugin file.

2) admin reports stock section custom tab:

here functions add custom tab corresponding custom content: list of product categories , each of them related products stock quantity sum.

// add custom tab 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; }  // content custom tab 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() $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 } 

code goes in function.php file of active child theme (or theme) or in plugin file.

this code tested on woocommerce 3+ , works.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -