【发布时间】:2014-11-18 02:47:41
【问题描述】:
我一直在四处寻找,似乎找不到任何东西。现在我有一个查询要显示我商店中的产品。我还需要能够在查询中显示每种产品的总销售额和每种产品的总零售额。我用它来创建排行榜。
总销售额很简单,这只是 woocommerce 为您设置的自定义字段。我很难找到零售总额的答案。
我还需要显示整个商店的总零售额,但似乎也没有什么办法。
this page 上的代码可以正常工作,但随后插件更新并停止运行。它只是显示 0,现在什么也没有显示。
这是我现在的查询,使用上述页面中的代码。
<?php $my_query = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => -1 ) );?>
<?php if ( $my_query->have_posts() ) : ?>
<?php $order_items = apply_filters( 'woocommerce_reports_top_earners_order_items', $wpdb->get_results( "
SELECT order_item_meta_2.meta_value as product_id, SUM( order_item_meta.meta_value ) as line_total FROM {$wpdb->prefix}woocommerce_order_items as order_items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta_2 ON order_items.order_item_id = order_item_meta_2.order_item_id
LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID
LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
LEFT JOIN {$wpdb->terms} AS term USING( term_id )
WHERE posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "')
AND order_items.order_item_type = 'line_item'
AND order_item_meta.meta_key = '_line_total'
AND order_item_meta_2.meta_key = '_product_id'
GROUP BY order_item_meta_2.meta_value
" )); ?>
<?php while($my_query->have_posts()): $my_query->the_post(); ?>
<div id="<?php $totalDonations = 0;
$Charities = array($post->ID);
//Gather Total for Individual Items
foreach ($order_items as $item) {
if (in_array($item->product_id, $Charities)) {
$totalDonations = $item->line_total + $totalDonations;
}
} echo $totalDonations; ?>" class="sort_by_total">
<div class="one_half">
<li><span><?php the_title(); ?></span></li>
</div>
<div class="one_quarter">
<p><?php echo get_post_meta($post->ID, 'total_sales', true); ?></p>
</div>
<div class="one_quarter last">
<p>$<?php echo $totalDonations; ?></p>
</div>
<div class="clear"></div>
<div class="divider"></div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php wp_reset_query();?>
一旦它们显示出来,我将使用总零售额作为每个 div 的 id,并以此为基础使用 jquery 对排行榜进行排序。这就是为什么它在代码中被调用了两次。
感谢任何帮助。谢谢
编辑:我一直在检查错误消息,我得到:PHP Fatal error: Using $this when not in object context on line 33 第 66 行也有同样的错误。第 33 行是以 <?php $order_items = apply_filters( 开头的行,第 66 行是以 @ 开头的行987654325@
编辑编辑:现在我没有收到任何错误消息。
【问题讨论】:
-
总销售额和“零售”总销售额有什么区别?
-
总销售额是销售的特定产品的数量。零售总额是明智的销售总额。因此,如果有两个特定产品以每件 10.00 的价格出售,那么它应该提供 20。
标签: php mysql wordpress woocommerce