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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。