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.

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

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启