0
votes

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. I have this top part fetching all orders belonging to the user within the last 90 days.

However, it returns 0 orders even though my test user has 1 completed order. I had a hunch it may be this line: 'before' => date('Y-m-d', strtotime('now')) So I removed it, and then it returned "19 orders", which is just freakin' impossible. 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!

Any help on this issue would be so much appreciated! All echos and print_r are there for debug purposes.

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:

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 ) )

Yet the $total shows: Orders: 19

Where is the 19 coming from !!!! lol

1
Have you dumped the output of $customer_orders before you pass it to the foreach() ?ProEvilz
I incorrectly assumed there'd be no problem with that. It returns only "Array ( )". I had removed the "before" part of date_query, and it did show a load of metadata from my 1 order though.user9229528

1 Answers

0
votes

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;