简体   繁体   English

WordPress的date_query表现奇怪

[英]Wordpress date_query behaving strangely

something of a noob here. 这里有点菜鸟。 I am trying to write some code to determine whether the current user has: An account older than 30 days AND 0 woocommerce orders. 我正在尝试编写一些代码来确定当前用户是否具有:帐户早于30天且woocommerce订单为0。 I have this top part fetching all orders belonging to the user within the last 90 days. 我的顶部是在过去90天内获取属于该用户的所有订单。

However, it returns 0 orders even though my test user has 1 completed order. 但是,即使我的测试用户有1个已完成的订单,它也会返回0个订单。 I had a hunch it may be this line: 'before' => date('Ym-d', strtotime('now')) So I removed it, and then it returned "19 orders", which is just freakin' impossible. 我有一种预感,可能是这行:'before'=> date('Ym-d',strtotime('now'))所以我将其删除,然后返回“ 19个订单”,这简直是不可能。 The print_r section seems to just show the garbled metadata of 1 order, so where it gets 19 from, I just can't understand! print_r部分似乎只显示1阶乱码的元数据,所以从19那里得到的数字我还是听不懂!

Any help on this issue would be so much appreciated! 在这个问题上的任何帮助将不胜感激! All echos and print_r are there for debug purposes. 所有回显和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' );

The print_r shows this: print_r显示如下:

Array ( [0] => WP_Post Object ( [ID] => 75675 [post_author] => 1 [post_date] => 2018-01-17 12:16:28 [post_date_gmt] => 2018-01-17 12:16:28 [post_content] => [post_title] => Order – January 17, 2018 @ 12:16 PM [post_excerpt] => [post_status] => wc-completed [comment_status] => open [ping_status] => closed [post_password] => order_5a5f3e9c442d6 [post_name] => order-jan-17-2018-1216-pm [to_ping] => [pinged] => [post_modified] => 2018-01-17 12:17:24 [post_modified_gmt] => 2018-01-17 12:17:24 [post_content_filtered] => [post_parent] => 0 [guid] => https://www.sheersense.com/?post_type=shop_order&p=75675 [menu_order] => 0 [post_type] => shop_order [post_mime_type] => [comment_count] => 3 [filter] => raw ) ) 数组([0] => WP_Post对象([ID] => 75675 [post_author] => 1 [post_date] => 2018-01-17 12:16:28 [post_date_gmt] => 2018-01-17 12:16 :28 [post_content] => [post_title] =>订单– 2018年1月17日下午12:16 [post_excerpt] => [post_status] => wc-completed [comment_status] =>打开[ping_status] =>关闭[post_password ] => order_5a5f3e9c442d6 [post_name] => order-jan-17-2018-1216-pm [to_ping] => [pinged] => [post_modified] => 2018-01-17 12:17:24 [post_modified_gmt] => 2018-01-17 12:17:24 [post_content_filtered] => [post_parent] => 0 [guid] => https://www.sheersense.com/?post_type=shop_order&p=75675 [menu_order] => 0 [post_type ] => shop_order [post_mime_type] => [comment_count] => 3 [filter] => raw))

Yet the $total shows: Orders: 19 然而, $total显示: Orders: 19

Where is the 19 coming from !!!! 19来自哪里!!!! lol 大声笑

Try like this. 尝试这样。

    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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM