0

I'm developing a system where user can buy scratch card number using pre-paid balance. I already created balance system using user meta. Now I need to delivery card number to users upon payment. As payment system is ready, I need an idea which will help me to deliver card number. Card numbers will be stored in database. I'll deliver them. Custom post type can be used probably but couldn't recognize how I deliver them.

For instance, User want buy a card of $10. I already stored 5 cards of $10, 3 cards of $20. If user make payment, system will deliver the card number which wasn't previously sold.

I need idea. Please suggest me.

Update: I got an idea. I'll create a custom post type called 'Card' with three meta key fields called 'Amount', 'Number' and 'Status'. When a specific function will run, a query will be run for searching the post type for certain amount and where Status will be "Unused". Then an email will be sent to the user with the card number and custom field 'Status' will be updated to "Used". Now I need an idea to search the post. I appreciate your help.

2 Answers 2

2

Here is an idea how we can sort the post based on custom field value and also I'd given some functions to change/delete custom field values.

<?php
$args = array(
    'post_type' => 'card', // custom post type name - card
    'meta_query' => array(
        'relation' => 'AND', // return post with meta-field key status = ununsed & amount = 10
        array(
            'key' => 'status',
            'value' => 'unused',
        ),
        array(
            'key' => 'amount',
            'value' => 10           // Array usage to show mulitple values- 'value' => array( 10, 20 ) 
        )
    )
);
$the_query = new WP_Query($args);
//this will show list of all available cards
while ( $the_query->have_posts() ) : $the_query->the_post();

if(/* condition paid */) {
$number = get_post_meta($post->ID, 'number', true);
echo $number; // or send email then

//change status from unused to used
update_post_meta($post_id, number, used, $number);

//delete amount custom field
delete_post_meta($post_id, amount, 10);

} else {
//do stuff if not paid
}
endwhile;
?>
3
  • Thanks. You missed $the_query = new WP_Query($args);
    – IFightCode
    Commented Aug 2, 2012 at 4:08
  • :) Your answer really helped me to figured out the way to work. Thanks again.
    – IFightCode
    Commented Aug 2, 2012 at 7:58
  • I'm glad I was able to help
    – Amit Kosti
    Commented Aug 2, 2012 at 8:00
1

Too easy, follow Amit's suggestion with

$args = array(
    'post_type' => 'card', // custom post type name - card
    'posts_per_page=1',
    'meta_query' => array(
        'relation' => 'AND', // return post with meta-field key status = ununsed & amount = 10
        array(
            'key' => 'status',
            'value' => 'unused',
        ),
        array(
            'key' => 'amount',
            'value' => 10           // Array usage to show mulitple values- 'value' => array( 10, 20 ) 
        )
    )
);
1
  • Yes, I did that with 'posts_per_page' => 1,
    – IFightCode
    Commented Aug 2, 2012 at 4:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.