【发布时间】:2013-10-23 21:36:11
【问题描述】:
我正在尝试编写自己的插件来扩展我们在 Wordpress 上的 Woocommerce 商店的报告。简而言之,我的结帐页面上有几个自定义字段,我希望管理屏幕中的报告显示我们在结帐时使用的自定义下拉列表中使用了每个 * 选项的次数。我们下拉列表的一个例子是“你是怎么知道我们的?”当然还有选择选项,他们可以在其中选择他们是如何了解我们的。
在我的插件中,我成功进入了 woocommerce 的报告部分,并拥有了我想要完成的基本线框。报告屏幕显示了一个表格,在一列上有用户看到的可用选项,如“广播广告”、“从朋友那里听到”等。右侧我只想知道该选项的总次数被使用了。我能够连接到数据库以获取这些值,但我似乎无法弄清楚如何计算这些选项被使用了多少次?
这是我目前所拥有的,它向我展示了所有可用的选项,但没有显示每个选项的使用量,我不确定如何集成?
function custom_reporting() {
global $start_date, $end_date, $woocommerce, $wpdb;
$start_date = isset( $_POST['start_date'] ) ? $_POST['start_date'] : '';
$end_date = isset( $_POST['end_date'] ) ? $_POST['end_date'] : '';
if ( ! $start_date )
$start_date = date( 'Ymd', strtotime( date('Ym', current_time( 'timestamp' ) ) . '01' ) );
if ( ! $end_date )
$end_date = date( 'Ymd', current_time( 'timestamp' ) );
$start_date = strtotime( $start_date );
$end_date = strtotime( $end_date );
$sql = "SELECT *
FROM {$wpdb->prefix}woocommerce_order_items AS order_items
LEFT JOIN {$wpdb->postmeta} AS country
ON order_items.order_id = country.post_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 country.meta_key = 'reason_for_purchase'
AND term.slug IN ('completed','processing','on-hold')
AND post_date > '" . date('Y-m-d', $start_date ) . "'
AND post_date < '" . date('Y-m-d', strtotime('+1 day', $end_date ) ) . "'
GROUP BY country.meta_value";
$result = $wpdb->get_results($sql, ARRAY_A);
?>
<form method="post" action="">
<p><label for="from"><?php _e('From:'); ?></label><input type="text" name="start_date" id="from" readonly="readonly" value="<?php echo esc_attr( date('Y-m-d', $start_date) ); ?>" /><label for="to"><?php _e('To:'); ?></label> <input type="text" name="end_date" id="to" readonly="readonly" value="<?php echo esc_attr( date('Y-m-d', $end_date) ); ?>" /><input type="submit" class="button" value="<?php _e('Show'); ?>" /></p>
</form>
<script type="text/javascript">
jQuery(function(){
<?php woocommerce_datepicker_js(); ?>
});
</script>
<table class="widefat">
<thead>
<tr>
<th>Reason for Purchase</th>
<th>Total Entries</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Reason for Purchase</th>
<th>Total Entries</th>
</tr>
</tfoot>
<tbody>
<?php
foreach($result as $value) {
?>
<tr>
<td><?php echo $value['meta_value']; ?></td>
<td><?php echo $value['sale_total']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
帮助,我快到了:D
【问题讨论】:
标签: php mysql wordpress woocommerce