Reputation: 80
Bit of an odd one and not even sure this is possible or if there is a better way to do it.
Basically, I have a "job package" that holds various bits of data. The main one is something called CV Credits - the user has to buy "CV Credits" to view a cv. Once out of CV Credits they have to buy more. The more complicated thing is that they can have many different packages at the same time.
So my logic was to check each package , check the first and use up the credits there first, then the second and so on. If none, they have to buy - with me?
Ok so here is my code (which only gathers the information from the first package) it is perfect, but how do I adapt the code to check the second when the variable $how_many_left is = "0"
php
$first = true;
foreach ( $packages as $package ) {
if ( $first ) {
$package = wc_paid_listings_get_package( $package );
$package_id = $package->get_id();
$cv_credit = $wpdb->get_row( "SELECT cv_credit, cv_credit_count, cv_credit_duration, date_purchased, credit_value FROM `wp_wcpl_user_packages` WHERE id = $package_id" );
$amount_of_credits = $cv_credit->cv_credit; // AMOUNT OF CREDITS THE PACKAGE HAS
$how_many_used = $cv_credit->cv_credit_count; // THE AMOUNT THEY HAVE USED
$credit_value = $cv_credit->credit_value; // WHAT THEY HAVE SEEN (COMMA SEPARATED)
$how_many_left = $amount_of_credits - $how_many_used;
$credit_duration = $cv_credit->cv_credit_duration;
$date_purchased = $cv_credit->date_purchased;
$timestamp = strtotime($date_purchased);
$date_purchased_yday = date("d", $timestamp); // day of the year
$dayend = $date_purchased_yday + $credit_duration - 1; // when it ends
$today = getdate();
$today_yday = $today["yday"]; // todays day of the year
$timeleft = $dayend - $today_yday;
if ($timeleft >= "0") {
if ($how_many_left >= "1") {
$createarrayofvalues = explode(',', $credit_value);
if (in_array($post->ID, $createarrayofvalues)) {
?>
<div class="contact-information">
<p>Mobile Number: <?php echo $mobile_number; ?> </p>
<p>Email Address: <?php echo $email; ?> </p>
<p>Links:
<?php the_resume_links(); ?>
</p>
</div>
<?php
} // IF they have paid for the CV before
else {
?> <a href="#" class="et_pb_button cv_credit">View contact information (<?php echo $how_many_left ?> credit/s left)</a> <?php
}
} // how many left
else {
echo "You have no more credits for this package";
}
}// time left on package
else {
}
$first = false;
} // if First
else {
}
}
I then use ajax to update the database with another CV count and other bits of information when they click on the "view contact information" button if they have credits and havent looked at the contact details before.
I do hope I am making sense, please ask any questions as there is information in there that is part of the code but doesnt necessary need to be there for this example.
To confirm: I want to check the first package in the loop - once there are no more credits to use, I then move onto the next package and so on.
Thanks in advance
Upvotes: 0
Views: 121
Reputation: 457
Basically findPackageWithCredits
looks for a package that has credits left or returns null
. This could still be made a lot nicer, but maybe it gives you something to start with ;)
<?php
handle($packages);
function handle($packages)
{
global $post;
$package = findPackageWithCredits($packages);
if (null === $package) {
echo "no credits left, buy more!";
return;
}
if (in_array($post->ID, explode(',', $package->credit_value))) {
showContact();
return;
}
showCVInfo($package->cv_credit - $package->cv_credit_count);
}
function findPackageWithCredits($packages)
{
foreach ($packages as $package) {
$db_package = getValidatedDatabasePackage($package);
if (null !== $db_package) {
return $db_package;
}
}
return null;
}
function getValidatedDatabasePackage($package)
{
global $wpdb;
$package = wc_paid_listings_get_package($package);
$package_id = $package->get_id();
$cv_credit = $wpdb->get_row("SELECT cv_credit, cv_credit_count, cv_credit_duration, date_purchased, credit_value FROM `wp_wcpl_user_packages` WHERE id = $package_id");
$amount_of_credits = $cv_credit->cv_credit; // AMOUNT OF CREDITS THE PACKAGE HAS
$how_many_used = $cv_credit->cv_credit_count; // THE AMOUNT THEY HAVE USED
$how_many_left = $amount_of_credits - $how_many_used;
$credit_duration = $cv_credit->cv_credit_duration;
$date_purchased = $cv_credit->date_purchased;
$timestamp = strtotime($date_purchased);
$date_purchased_yday = date("d", $timestamp); // day of the year
$dayend = $date_purchased_yday + $credit_duration - 1; // when it ends
$today = getdate();
$today_yday = $today["yday"]; // todays day of the year
$timeleft = $dayend - $today_yday;
if ($timeleft >= "0" && $how_many_left >= "1") {
return $cv_credit;
}
return null;
}
function showContact()
{
global $mobile, $email, $links;
$message = '<div class="contact-information">
<p>Mobile Number: %s </p>
<p>Email Address: %s </p>
<p>Links: %s
</p>
</div>';
echo sprintf($message, $mobile, $email, $links);
}
function showCVInfo($remaining_credits)
{
$message = '<a href="#" class="et_pb_button cv_credit">View contact information (%s credit/s left)</a>';
echo sprintf($message, $remaining_credits);
}
Upvotes: 1