0

I keep an order list of all users. I keep this list in the meta_key value named payments. The payments meta allows me to keep information about the payments the user made. To give an example for an user:

array(
    0 => array(
        'date' => '2020-04-15',
        'amount' => '750',
    ),
    1 => array(
        'date' => '2020-04-20',
        'amount' => '900',
    ),
    2 => array(
        'type' => 'installment',
        'installments' => array(
            0 => array(
                'date' => '2020-04-20',
                'amount' => '250',
            ),
            1 => array(
                'date' => '2020-05-20',
                'amount' => '250',
            ),
            2 => array(
                'date' => '2020-06-20',
                'amount' => '250',
            )
        )
    )
)

Based on this information, I try to list the users who have not paid. How can I list users with a payment older than today? I tried this but it didn't:

$today = date('Y-m-d');    
$wpdb->get_results("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'payments' AND meta_value <= '%".$today."%'");

Alternative scenario:

Based on the comment made by @Himand, I produced an alternative scenario. I decided to keep each of a user's payments as a separate meta_value. For example:

For user 1:

I created a metadata that holds the total number of payments: _payments_count

And I gave a number to each payment in order. For example: _payment_detail_1 _payment_detail_2 _payment_detail_3.

Then I created a for loop depending on the number of payments.

$payment_count = get_user_meta( $user_id, '_payments_count', true );
for ($i = 1; $i <= $payment_count ; $i++) {
    $payment_detail = get_user_meta( $user_id, '_payment_detail_'.$i, true );
}

This way I can access all payment data, but I just don't know what to do when I want to list older payments than today. After I pull all the payment information, I can make it list the older ones today with an if statement. Like this:

$payment_count = get_user_meta( $user_id, '_payments_count', true );
for ($i = 1; $i <= $payment_count ; $i++) {
    $payment_detail = get_user_meta( $user_id, '_payment_detail_'.$i, true );
    if ( $payment_detail['payment_date'] < date('Y-m-d') ){
        // ......
    }
}

However, this means a big loss of performance. Because there are thousands of users registered in the system. I want to list the payments by comparing the date information directly.

Anyone have an idea about this?

11
  • The way you're doing it is not optimal for search queries, you should have a separate meta entry for each payment record.
    – Himad
    Commented May 10, 2020 at 0:55
  • @Himad Thank you for your suggestion. So if I keep the payment information as you say, how can I sort past payments by date?
    – Faruk
    Commented May 10, 2020 at 9:38
  • @Himad Based on your comment, I added an alternative scenario. Is it possible for you to review?
    – Faruk
    Commented May 10, 2020 at 10:50
  • Using a separate meta key for each payment is not optimal at all. Ideally, you would treat a payment as a custom post type. The post creation date is the payment date, the author is the user who made the payment and then all you'd have to do is add a custom meta field to the post representing the payment amount. After that, you can search your payments using a WP_Query which can be configured to search by date.
    – Himad
    Commented May 10, 2020 at 14:05
  • @Himad Thank you for your interest and for your descriptive answers. Payments can be both in cash and in installments. In installment payment types, I keep the payment date of each installment separately. In this case, it is impossible to keep the installment date as post creation date. Because payment dates may be in the future, now or in the past. Do you have any idea for this situation? I added an example of installment payment to the question.
    – Faruk
    Commented May 10, 2020 at 14:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.