skip to Main Content

I have elaborated the attached code, in order to recall all the products purchased by a user. At this point, however, I would like to insert a condition such as: show only the products purchased in the last 3 days. However, I have never ventured into ifs by date. Anyone have any idea how to create such a condition?

<?php

          $order_status = array_keys( wc_get_order_statuses());
                  $customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
                    'numberposts' => - 1,
                    'meta_key'    => '_customer_user',
                    'meta_value'  => get_current_user_id(),
                    'post_type'   => wc_get_order_types( 'view-orders' ),
                    'post_status' => $order_status,
                ) ) );

                foreach ( $customer_orders as $customer_order ) {
                    $order = new WC_Order( $customer_order );
          foreach ( $order->get_items() as $item_id => $item ) :
          endforeach;
                    $date = esc_html( wc_format_datetime( $order->get_date_created() ) );
?>

          <?php $date_comp = $order->get_date_completed(); ?>
          <?php $product = wc_get_product( $item->get_product_id() ); ?>
          <?php $product_id = $item->get_product_id(); ?>
}

2

Answers


  1. I used to select orders using these conditions

    'date_query' => array(
       'after' => date('Y-m-d', strtotime('-3 days'))
    )
    

    'date_query' => array(
     array(
      'after' => $date_start,
      'before' => $date_end,
      'inclusive' => true,
     ),
    ),
    
    Login or Signup to reply.
  2. You can use wc_get_orders where you can pass date_created param. for more information check here – WC_Order_Query. try the below code.

    // Get last 3 days orders.
    $args = array(
        'numberposts'  => -1,  
        'orderby'      => 'date',
        'order'        => 'DESC',  
        'customer_id'  => get_current_user_id(),
        'date_created' => '>' . date( 'Y-m-d', strtotime('- 3day' ) ),
        'customer_id'  => get_current_user_id(),
    );
    $customer_orders = wc_get_orders( $args );
    
    //* Loop through each WC_Order object
    foreach( $customer_orders as $customer_order ){
    
        $order = new WC_Order( $customer_order );
    
        foreach ( $order->get_items() as $item_id => $item ) :
    
        endforeach;
        
        $date       = esc_html( wc_format_datetime( $order->get_date_created() ) );
        $date_comp  = $order->get_date_completed();
        $product    = wc_get_product( $item->get_product_id() );
        $product_id = $item->get_product_id();
    
    }
    

    OR you can use date_query in get_posts.

    <?php
    
    $order_status     = array_keys( wc_get_order_statuses());
    $customer_orders  = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
        'numberposts' => - 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => wc_get_order_types( 'view-orders' ),
        'post_status' => $order_status,
        'date_query'  => array(
            'after'   => date( 'Y-m-d', strtotime('-3 days') )
        )
    ) ) );
    
    foreach ( $customer_orders as $customer_order ) {
        $order = new WC_Order( $customer_order );
        foreach ( $order->get_items() as $item_id => $item ) :
        endforeach;
        $date = esc_html( wc_format_datetime( $order->get_date_created() ) ); ?>
        <?php $date_comp = $order->get_date_completed(); ?>
        <?php $product = wc_get_product( $item->get_product_id() ); ?>
        <?php $product_id = $item->get_product_id(); ?>
    }
    
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search