Skip Navigation

[Resolved] Not empty condition not working

This support ticket is created 3 years, 7 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 7 replies, has 2 voices.

Last updated by roxanneS 3 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#2276379
Screenshot-2022-01-26-092953.jpg

Tell us what you are trying to do?
Calculate the number of days to harvest (PARENT slug: plante-recolte) in the hydroponics system based on transplant date (slug: semis-date-transplantation).

Under Toolet > Custom code I added this snippet
/**
* Calculate the number of days to harvest (PARENT slug: plante-recolte) in the DFT system based on transplant date (slug: semis-date-transplantation)
The shortcode will look like this
[dft-harvest-date date_transplant="[types field='semis-date-transplantation' style='text' format='U'][/types]" days_harvest="[types field='plante-recolte' item='@plante-semences.parent'][/types]"]
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_shortcode('dft-harvest-date', function($atts, $content){
$atts = shortcode_atts( array(
'date_transplant' => '',
'days_harvest' => ''
), $atts );
$res = '';
if( !empty($atts['date_transplant']) && !empty($atts['days_harvest']) ){
$ts = strtotime( '+' . $atts['days_harvest'] . ' days', $atts['date_transplant']);
}
$res = date_i18n( 'j-M-Y', $ts );
return $res;
});

In a page , I inserted a view using the shortcode [dft-harvest-date date_transplant="[types field='semis-date-transplantation' style='text' format='U'][/types]" days_harvest="[types field='plante-recolte' item='@plante-semences.parent'][/types]"]

Result: The "not empty" condition does not work. The view displays the current date when the date_transplant or the days_harvest is empty (As shown next to Récolte prévue: ). I tried adding a conditional block to the view with the "not empty" condition. Result is the same.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?
hidden link

#2276671

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roxanne,

Thank you for getting in touch.

What i'm seeing here is that the code isn't working correctly.

Even if a date value is provided it is not correctly picking up the value.

Would you mind allowing me to have admin access to the site as well as a link to the page with the issue.

Thanks,
Shane

#2276729

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roxanne,

Thank you for the credentials.

Checking on the code there seems to be nothing wrong. Is it a case where the Jours pour récolte: field is not displaying anything ?

If that is the case then this is happening because the parent field that it is pulling this data from doesn't have any value inside it.

It's getting the value from the Récolte - transplantation (jours) field.

Thanks,
Shane

#2276741

I'm not sure I understand your observation.

In the screen shot I provided, I inserted the following fields :
Transplanté: (date) (this a single field representing the transplant date)
Jours pour récolte : (number) (this is the number of days to harvest - pulled from the plant relationship)
Récolte prévue: (shortcode) This is where I inserted the shortcode

If either the transplant date or days to harvest are empty, Récolte prévue should be empty. In the screenshot, you can see that it's returning today's date when a field is empty. Meaning it's not correctly assessing the Not IsEmpty condition.

#2277647

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roxanne,

I'm struggling to find where the custom field below is located.
[types field='plante-recolte' item='@plante-semences.parent'][/types]

I'm not seeing it on the parent post type (Semis) . Can you point me to exactly where this field is. I'm doing a trace of the field to find out what value is being stored in the field.

Thanks,
Shane

#2277737

That custom field is in custom post Plante (slug: plante) - under field group Guide de culture (culvitation guide).

In Summary: I created a custom post for plants (slug:plante) and another custom post for seedlings (slug: semis). Then, I created a one-to-many relationship (slug: plante-semences)

The plant contains the information about the number of days to harvest. The seedling contains the date of transplant of the seedling.

#2277935

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Roxanne,

After further checks the issue is not that the dates were empty. It's that you were calling the date_i18n function outside the if statement and in this case it will default to Today's date when no timestamp is provided.

The correct code is below.

*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_shortcode('dft-harvest-date', function($atts, $content){
$atts = shortcode_atts( array(
'date_transplant' => '',
'days_harvest' => ''
), $atts );
$res = '';
if( !empty($atts['date_transplant']) && !empty($atts['days_harvest']) ){
$ts = strtotime( '+' . $atts['days_harvest'] . ' days', $atts['date_transplant']);
$res = date_i18n( 'j-M-Y', $ts );
}

return $res;
});

In a page , I inserted a view using the shortcode [dft-harvest-date date_transplant="[types field='semis-date-transplantation' style='text' format='U'][/types]" days_harvest="[types field='plante-recolte' item='@plante-semences.parent'][/types]"]

Given that the date should only be there if both fields are not empty then placing the date function inside the if condition as well will ensure this.

Thanks,
Shane

#2277939

My issue is resolved now. Thank you!