Hi all.
I have a library system where I have a checkbox Types custom field (prestamo) to say if the book is checked out. If is marked, it shows a div noticing that the book is unavailable. By other side, I have another conditional that check if the user is logged in to show or not the apply for the book form.
hidden link hidden link
Now I need a second conditional to make the form disappear when this custom field is checked and the book is unavailable. All I need is to group these two conditionals together. Any help will be fully appreciated. Thanks.
Something like that, but is not working:
<?php
if ( types_render_field('prestamo') ) || ('is_user_logged_in') {
echo '<div class="pages-login-container">'
echo '<h3>_e('Solicitar Libro', 'pajarita')</h3>'
echo '<p> Para solicitar este libro es necesario acceder al sistema. Utilize sus credenciales para rellenar el formulario abajo.</p>'
echo 'do_shortcode('[wpv-login-form]');'
echo '</div>;'
}
else {
}
?>
************************************************
And here my actual full code on the theme.
Can I just clarify what you want to appear in the various scenarios to make sure we are aiming for the same thing.
There are two things you are testing for (book availability, client status) so four possible scenarios:
1. book not available (lent out), user not logged-in
2. book not available (lent out), user logged-in
3. book available (not lent out), user not logged-in
4. book available (not lent out), user logged-in
Can you confirm what should be output in each case?
And, importantly, the setting of the checkbox field, it saves '0' to the database if unchecked, or saves nothing? That will affect how empty($value) is evaluated.
Hi Nigel.
You're right, I have four possibilities here. But actually this is the main scenario.
1 - Either when user is logged out OR logged in AND the book is LENT OUT never appears the book request form: <?php echo do_shortcode('[contact-form-7 id="17723" title="Biblioteca"]'); ?>
2 - If the book is NOT LENT OUT AND the user is logged in, the request form appears.
3 - If user is logged out the request form never apears. (This actually works right now)
But I still have problems to make that based on the types lent checkbox: if ( types_render_field('prestamo') ) to make the step 1 and 2.
If you'd prefer to use a combined condition for the logged-in status and the 'prestamo' field's checked status, here is how it will look:
global $post;
// get the 'prestamo' field value
$value = types_render_field('prestamo',array('output'=>'raw', 'item'=>$post->ID));
// if the user is logged-in and the 'prestamo' field is not checked
if ( (is_user_logged_in()) && ($value != '1') ) {
// show the book lending form
echo 'show the book lending form';
}
Important note: In this example code snippet, I'm evaluating the 'prestamo' field's value with '1' because that is what I have saved in that field's 'Value to store' setting.
( screenshot: hidden link )
If your field is set to store a value other than '1', you'll replace it in the code snippet.
Based on the code that you've shared, it seems that you also need to show different messages/content based on the user's logged-in status and the status of the 'prestamo' field. In that case, it would be better to use, nested conditions, instead of a combined condition, for example:
global $post;
// check the user's logged-in status
if ( is_user_logged_in() ) {
// if the user is logged in, get the 'prestamo' field value
$value = types_render_field('prestamo',array('output'=>'raw', 'item'=>$post->ID));
// if the 'prestamo' field is not checked
if ( $value != '1' ) {
// show the book lending form
echo 'The book lending form';
}
else
{
// show the book unavailable message
echo 'This book is not available';
}
}
else
{
// show the message to login and the login form, as needed
echo "You're not logged in";
}
I hope this helps and please let me know if you need any further assistance around this.
Hi Waqar.
It's absolutely great, thank you very much. At the end the first, and more simple solution, fits my needs. Sorry for the delay, my client was on vacation and I need their aprovall.
My issue is resolved now. Thank you!