但是,即使我的测试用户有1个已完成的订单,它也会返回0个订单。 我有预感可能是这一行: '前' =>日期(' Y-m-d',strtotime('现在')) 所以我删除了它,然后它返回" 19个订单",这只是怪胎'不可能。 print_r部分似乎只显示1个订单的乱码元数据,所以它从哪里得到19,我就无法理解!
对此问题的任何帮助都将非常感谢!所有的echos和print_r都可以用于调试目的。
function woo_reg_matured( $days_old = 30 )
{
$cu = wp_get_current_user();
return ( isset( $cu->data->user_registered ) && strtotime( $cu->data->user_registered ) < strtotime( sprintf( '-%d days', $days_old ) ) ) ? TRUE : FALSE;
}
function woo_inactive_shortcode( $atts = array(), $content = '' ) {
if ( is_user_logged_in() ):
$customer_orders = get_posts( array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => array( 'shop_order' ),
'post_status' => array( 'wc-completed' ),
'date_query' => array(
'after' => date('Y-m-d', strtotime('-90 days'))
) ) );
$total = 0;
print_r($customer_orders);
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$total += $order->get_total();
}
echo 'Orders: ' . $total . '<br>';
if ($total <= 5) {
if( woo_reg_matured( $days_old = 30 ) ) {
echo 'You are inactive with ' . $total . ' orders and account age over 30 days';
return;
}
echo 'You are inactive with ' . $total . ' orders and account age under 30 days';
return;
}
echo 'You are active with over 30 days age and ' . $total . ' orders';
return;
endif;
}
add_shortcode( 'woo_inactive', 'woo_inactive_shortcode' );
print_r显示了这个:
数组([0] =&gt; WP_Post对象([ID] =&gt; 75675 [post_author] =&gt; 1 [post_date] =&gt; 2018-01-17 12:16:28 [post_date_gmt] =&gt; 2018-01-17 12:16:28 [post_content] =&gt; [post_title] =&gt;订单 - 2018年1月17日@ 12:16 PM [post_excerpt] =&gt; [post_status] =&gt; wc-completed [comment_status ] =&gt;打开[ping_status] =&gt;关闭[post_password] =&gt; order_5a5f3e9c442d6 [post_name] =&gt; order-jan-17-2018-1216-pm [to_ping] =&gt; [pinged] =&gt; [post_modified ] =&gt; 2018-01-17 12:17:24 [post_modified_gmt] =&gt; 2018-01-17 12:17:24 [post_content_filtered] =&gt; [post_parent] =&gt; 0 [guid] =&gt; { {3}} [menu_order] =&gt; 0 [post_type] =&gt; shop_order [post_mime_type] =&gt; [comment_count] =&gt; 3 [filter] =&gt; raw))
然而$total
显示:Orders: 19
19来自哪里!!!!洛尔
答案 0 :(得分:0)
试试这样。
if ( is_user_logged_in() ):
$customer_orders = get_posts( array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => array( 'shop_order' ),
'post_status' => array( 'wc-completed' ),
'date_query' =>
array('after' =>
array(
'year' => 2017,
'month' => 03,
'day' => 12,
),
'before' =>
array(
'year' => 2018,
'month' => 01,
'day' => 17,
),
) ) );
$total = 0;
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
print_r($customer_order);
//$total++;
$total += $order->get_total();
}
echo 'Orders: ' . $total . '<br>';
if ($total <= 5) {
/* if( woo_reg_matured( $days_old = 30 ) ) {
echo 'You are inactive with ' . $total . ' orders and account age over 30 days';
return;
}*/
echo 'You are inactive with ' . $total . ' orders and account age under 30 days';
return;
}
echo 'You are active with over 30 days age and ' . $total . ' orders';
return;
endif;