Skip Navigation

[Resolved] CUSTOM FIELD CONDITIONAL

This support ticket is created 2 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 3 voices.

Last updated by Paulo Ramalho 2 years, 9 months ago.

Assisted by: Waqar.

Author
Posts
#2415325

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.

<?php global $post;
$value = types_render_field('prestamo',array('output'=>'raw'));
if(!empty($value)) {
?>
<p class="prestamo-single">Status de préstamo <?php echo do_shortcode('[types field="prestamo"][/types]' ); ?></p>
<?php
} ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php if ( is_user_logged_in() ) { ?>
<?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('Area Solicitar Libro')) ?>
<?php echo do_shortcode('[contact-form-7 id="17723" title="Biblioteca"]'); ?>

<?php } else { ?>
<div class="pages-login-container">
<h3><?php _e('Solicitar Libro', 'pajarita'); ?></h3>
<p><?php _e('Para solicitar este libro es necesario acceder al sistema. Utilize sus credenciales para rellenar el formulario abajo.', 'pajarita'); ?></p>
<?php echo do_shortcode('[wpv-login-form]'); ?>
</div>
<?php } ?>
<?php endwhile; else: ?>
<?php endif; ?>

#2415353

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

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.

#2416653

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.

Thanks in advance Nigel.
Paulo

#2416837

Hi Paulo,

Thank you for sharing these details.

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.

#2419825

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!