按产品类别划分的总库存量在Woocommerce报告中

时间:2017-07-25 15:09:45

标签: php wordpress woocommerce custom-taxonomy stock

我想在Woocommerce中按类别获得总股票估值。

任何人都知道如何做到这一点?

我知道这是一个简单的代码,但我无法理解。

enter image description here

由于

1 个答案:

答案 0 :(得分: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 REPORTS STOCK SECTION CUSTOM TAB:

以下是将添加带有相应自定义内容的自定义标签的功能:您将获得所有产品类别的列表,并为每个产品类别列出所有相关产品库存数量总和。

// 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+上进行测试并正常运行。