dongnaizao8039 2017-02-06 22:01
浏览 38
已采纳

Woocommerce:按产品标签和产品类别查询总销售额?

I am completely at a loss on how to get some 'relatively' simple data from my woocommerce orders.

What I need is a query that allows me to simply plug in a category as well as a tag and return the total sales (as income [$]) so that I can do some simple math with the results and display the amount.

I am trying to have a storefront on my studios website that is broken down into categories by project "Webcomic", "Game", Etc. and then filter the sales of ANY of the product sales within the category by the tag "contribution" or "sales".

The purpose for this is to have a very rudimentary metric display on a project page which breaks down what I've invested in the project in time and money, and what I've earned from it in patronage and merch sales.

The idea being that I go:

  • Query: What are the total earnings of "$project" in "contributions"?
  • Query 2: What are the total earnings of "$project" in "sales"?
  • Display: "$project" has earned X in contributions and Y in sales. Totaling Z.

Screenshot of Metrics Widget

If not tags and categories, how would I go about giving my products custom fields that I'd be able to query through the orders?

Any help in this regard would be immensely helpful.

Added through further study:

This question asks WHERE the order data is being stored.

How would I go about querying the different chunks of data to trace a line to all the chunks I need?

Query shop_orders > Loop through Orders > push orders who's order_item has the right category and tags to an array

Is my initial thought. Would there be a more efficient way to do this?

  • 写回答

1条回答 默认 最新

  • dscuu86620 2017-02-10 22:07
    关注

    Using the source code from this plugin I was able to find the query information that I needed to get the proper data returned to me.

    I copied the function: public function awts_get_total_sales_per_product($product_id ='') from line 92 into my custom shortcodes file.

    I then made my own query:

    $args = array(
        'numberposts' => -1,
        'post_type' => 'product',
        'product_cat' => $shopSlug,
        'product_tag' => 'contribution', //contribution in one query, merch in the other.
        'orderby' => 'date',
        'order' => 'dsc');
    

    Then just did a foreach loop to sum all of the results:

    $query = get_posts( $args );
    
        foreach ( $query as $post ) : setup_postdata( $post );
            $var = awts_get_total_sales_per_product($post->ID);
            $contributed += $var->{_line_total};
        endforeach;
    

    I would like to thank the people who put the work into the Woo-Total-Sales plugin, I'm actually going to continue using it on the site. I was hoping I didn't have to hack apart a well made plugin to get what I needed but it's been 3 days and I really need to move on with my development.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求一跃动小子保卫主公算法实现
  • ¥15 地图软件开发技术答疑(api, 地点获取,外观样式)
  • ¥20 物理远程控制麦克风使用问题
  • ¥15 打印预览会泄漏纸钱包密码吗
  • ¥15 在hololens1上运行unity项目只有空窗口
  • ¥25 TABLEAU PREP无法打开
  • ¥15 百度帐号问题/centos
  • ¥15 关于#c语言#的问题:求完整代码条件好说
  • ¥100 HALCON DELPHI
  • ¥15 (需要远程,AI不回)VB6二进制文件转换成功,但是C#转换总是失败