2

I have some code which check the purchased date of a product in woocommerce.

Here is the code:

function _cmk_check_ordered_product( $id ) {
    // Get All order of current user
    $orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => wc_get_order_types( 'view-orders' ),
        'post_status' => array_keys( wc_get_order_statuses() )
    ) );

    if ( !$orders ) return false; // return if no order found

    $all_ordered_product = array(); // store products ordered in an array

    foreach ( $orders as $order => $data ) { // Loop through each order
        $order_data = new WC_Order( $data->ID ); // create new object for each order
        foreach ( $order_data->get_items() as $key => $item ) {  // loop through each order item
            // store in array with product ID as key and order date a value
            $all_ordered_product[ $item['product_id'] ] = $data->post_date; 
        }
    }
    if ( isset( $all_ordered_product[ $id ] ) ) { // check if defined ID is found in array
        return 'You purchased this product on '. date('M. d, Y', strtotime( $all_ordered_product[ $id ] ) );
    } else {
        return 'Product Never Purchased';
    }
}

So it just check the purchase date. Now I need to check if the current date is less than 15 days from the purchase date and if it is I echo: "this product is expired".

I am thinking about using PHP Date for this, something like if ($date < strtotime('-15 days')). But I am stuck. What is the right direction? How can I solve it?

Any help is very appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Aryabh
  • 77
  • 1
  • 11

1 Answers1

1

Below is a light and effective SQL query where you will get what you are expecting (and more):

  • It will check that the product has been already bought
  • It will check that the purchase date has been made in the defined number of remaining days.

As a customer can purchase multiple times a same iteme (product), the function will take in account the last purchase item for a defined product ID.

The code:

function check_ordered_product( $product_id = 0, $days = 15, $user_id = 0 ) {
    global $wpdb;
    $customer_id = $user_id == 0 || $user_id == '' ? get_current_user_id() : $user_id;
    $statuses    = array_map( 'esc_sql', wc_get_is_paid_statuses() );

    $time_n_days_ago = strtotime("-$days days");
    $found = $valid = false;

    // The query
    $results = $wpdb->get_col( "
        SELECT p.post_date FROM {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
        AND pm.meta_key = '_customer_user'
        AND pm.meta_value = $customer_id
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        AND woim.meta_value = $product_id
    " );

    if( count($results) > 0 ){
        $found = true;
        foreach( $results as $post_date ){
            if( strtotime($post_date) > $time_n_days_ago ){
                $valid = true;
                break;
            }
        }
    }
    if ( $found && ! $valid ) {
        $text = __( "This product has expired.", "woocommerce" );
    } elseif ( $found && $valid ) {
        $text = __( "This product is still valid.", "woocommerce" );
    } else {
        $text = __( "You have not purchased this product yet.", "woocommerce" );
    }
    echo '<p>'.$text.'</p>';
}

Usage (example) for a logged in user on front end.

For product ID 37 on past 15 days you will use:

check_ordered_product( 37, 15 );

It will display:

  • If product has been purchased after the 15 past days: This product has expired.
  • If product has been purchased before the 15 past days: This product is still valid.
  • If product has not been purchased yet: You have not purchased this product yet.

Related similar answer: WooCommerce - Check if user purchased a product in the past 60 days

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399