Support » Plugin: Business Directory Plugin | GeoDirectory » Logic for Business Hours?

  • Resolved EugeneUK

    (@euguk007)


    Hi.
    It is good to have Business Hours custom field, BUT:

    – Why did you set the main radio-button option as ‘No’ by default? Why not leave them both unchecked and add Required option in the CF Settings, so people could make their own mind on whether they are opened or closed, and whether the actually wish to include the hours?

    – Now, that you enforced the default ‘No’ – Why did you set Saturday and Sunday as ‘Closed’ and all other days ‘Open’ and preset with hours by default? Wouldn’t it be better to leave it for the user to start adding the relevant days and times?

    Where is the logic for this field?

    Hope this helps you to re-think this particular custom field.

Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Author Stiofan

    (@stiofansisland)

    Hi @euguk007

    We use the most common defaults to save a user time, if we set it to yes then a user might miss it and end up with the defaults which in some cases might be wrong.

    Google has BH set off by default and even default to every day closed.

    There is a filter to adjust the default times if you know better defaults for your industry, if you need an example of that just let me know 🙂

    Thanks,

    Stiofan

    Hi Stiofan, and fair enough answer 🙂
    Al the least, I would like to make that field a required one.
    If you have any solutions for this (which I am sure you have), I’d be very grateful. Thanks! Eugene

    Meaning – I would like both radio buttons both unchecked and required.

    Plugin Author Stiofan

    (@stiofansisland)

    Hi Eugene,

    There is not currently an easy way to do this.
    I think the only way would be to remove the output function and replace it with a new one, but this would mean any changes to the function in the future would not show.

    Stiofan

    Dear Stiofan,

    First, thank you for replying so swiftly.
    I am OK with changing the function. I can always update it in the future updates.
    Could you please direct me as to which function do I need to look at?

    Thanks,
    Eugene

    Dear Stiofan,
    So far I managed to achieve everything (I think) I need accept the display of required message error under the business hours field.
    What I have done:
    1. In /includes/admin/settings/class-geodir-settings-cpt-cf.php
    under Business Hours I set ‘is_required’ as true;
    2. In /includes/admin/settings/class-geodir-settings-cpt-cf-extras.php
    commented out

    // add_filter( 'geodir_cfa_is_required_business_hours', '__return_empty_string', 10, 4 );
    // add_filter( 'geodir_cfa_required_msg_business_hours', '__return_empty_string', 10, 4 );

    3. In /includes/custom-fields/input-functions.php
    in function geodir_cfi_business_hours( $html, $cf ) added
    <?php if ($cf['is_required']) echo '<span>*</span>'; ?>
    before the label closing, and:
    checked( ! empty( $value ), true );
    for the ‘No’ radio like it is for ‘Yes’ radio, and:

    <?php if ($cf['is_required']) { ?>
       <span class="geodir_message_error"><?php _e($cf['required_msg'], 'geodirectory'); ?></span>
    <?php } ?>

    before closing the last DIV

    Plugin Author Stiofan

    (@stiofansisland)

    Hi Eugene,

    You should never have to edit any core files.
    You can add the below code snippet via the “code snippets” plugin, i think it does what you want.

    // remove the original BH input
    remove_filter( 'geodir_custom_field_input_business_hours', 'geodir_cfi_business_hours', 10 );
    
    // Add our own required custom BH input
    add_filter( 'geodir_custom_field_input_business_hours', '_my_geodir_cfi_business_hours', 15, 2 );
    function _my_geodir_cfi_business_hours( $html, $cf ) {
        if ( empty( $html ) ) {
            $htmlvar_name = $cf['htmlvar_name'];
    		$name = $cf['name'];
    		$label = __( $cf['frontend_title'], 'geodirectory' );
    		$description = __( $cf['desc'], 'geodirectory' );
    		$value = geodir_get_cf_value( $cf );
    		
    		$weekdays = geodir_get_weekdays();
    		$hours = array();
    		$display = 'none';
    		$timezone_string = trim( get_option('timezone_string') );
    		$gmt_offset = esc_attr( geodir_utc_offset_dst( $timezone_string ) );
    
            if ( ! empty( $value ) ) {
    			$display = '';
    			$value = stripslashes_deep( $value );
    			$periods = geodir_schema_to_array( $value );
    			if ( ! empty( $periods['hours'] ) ) {
    				$hours = $periods['hours'];
    			}
    			if ( ! empty( $periods['offset'] ) ) {
    				$gmt_offset = $periods['offset'];
    			}
    		} else {
    			$hours = geodir_bh_default_values(); // Default value
    		}
    		
    		$is_edit = !empty($_REQUEST['pid']) || !empty($_REQUEST['post']) ? true : false;
    		
    		$time_format = geodir_bh_input_time_format();
    		ob_start();
    		
    		?>
    		<script type="text/javascript">jQuery(function($){GeoDir_Business_Hours.init({'field':'<?php echo $htmlvar_name; ?>','value':'<?php echo $value; ?>','json':'<?php echo stripslashes_deep(json_encode($value)); ?>','offset':'<?php echo $gmt_offset; ?>'});});</script>
            <div id="<?php echo $name;?>_row" class="geodir_form_row clearfix gd-fieldset-details gd-bh-row">
                <label for="<?php echo $htmlvar_name; ?>_f_active_1"><?php echo $label; ?></label>
    			<div class="gd-bh-field" data-field-name="<?php echo $htmlvar_name; ?>" role="radiogroup">
    				<span class="gd-radios" role="radio"><input name="<?php echo $htmlvar_name; ?>_f[active]" id="<?php echo $htmlvar_name; ?>_f_active_1" value="1" class="gd-checkbox" field_type="radio" type="radio" required <?php checked( ! empty( $value ), true ); ?> data-field="active" aria-label="<?php esc_attr_e( 'Yes', 'geodirectory' ); ?>"> <?php _e( 'Yes', 'geodirectory' ); ?></span> 
    				<span class="gd-radios" role="radio"><input name="<?php echo $htmlvar_name; ?>_f[active]" id="<?php echo $htmlvar_name; ?>_f_active_0" value="0" class="gd-checkbox" field_type="radio" type="radio" required <?php checked( $value == '' && $is_edit, true ); ?> data-field="active" aria-label="<?php esc_attr_e( 'No', 'geodirectory' ); ?>"> <?php _e( 'No', 'geodirectory' ); ?></span>
    				<div class="gd-bh-items" style="display:<?php echo $display; ?>" data-12am="<?php echo esc_attr( date_i18n( $time_format, strtotime( '00:00' ) ) ); ?>">
    					<table class="form-table widefat fixed">
    						<thead>
    							<tr><th class="gd-bh-day"><?php _e( 'Day', 'geodirectory' ); ?></th><th class="gd-bh-24hours"><?php _e( 'Open 24 hours', 'geodirectory' ); ?></th><th class="gd-bh-time"><?php _e( 'Opening Hours', 'geodirectory' ); ?></th><th class="gd-bh-act"><span class="sr-only"><?php _e( 'Add', 'geodirectory' ); ?></span></th></tr>
    						</thead>
    						<tbody>
    							<tr style="display:none!important"><td colspan="4" class="gd-bh-blank"><div class="gd-bh-hours"><input type="text" id="GD_UNIQUE_ID_o" data-field-alt="open" data-bh="time" aria-label="<?php esc_attr_e( 'Open', 'geodirectory' ); ?>" readonly> - <input type="text" id="GD_UNIQUE_ID_c" data-field-alt="close" data-bh="time" aria-label="<?php esc_attr_e( 'Close', 'geodirectory' ); ?>" readonly><input id="GD_UNIQUE_ID_oa" type="hidden" data-field="open"><input type="hidden" id="GD_UNIQUE_ID_ca" data-field="close"> <span class="gd-bh-remove"><i class="fas fa-minus-circle" aria-hidden="true"></i></span></div></td></tr>
    							<?php foreach ( $weekdays as $day_no => $day ) { ?>
    							<tr class="gd-bh-item<?php echo ( empty( $hours[ $day_no ] ) ? ' gd-bh-item-closed' : '' ); ?>">
    								<td class="gd-bh-day"><?php echo $day; ?></td>
    								<td class="gd-bh-24hours"><input type="checkbox" value="1"></td>
    								<td class="gd-bh-time" data-day="<?php echo $day_no; ?>" data-field="<?php echo $htmlvar_name; ?>_f[hours][<?php echo $day_no; ?>]">
    									<?php 
    										if ( ! empty( $hours[ $day_no ] ) ) {
    											$slots = $hours[ $day_no ];
    
    											foreach ( $slots as $slot ) {
    												$open = $close = $open_display = $close_display = $open_His = $close_His = '';
    
    												$unique_id = uniqid( rand() );
    
    												if ( ! empty( $slot['opens'] ) ) {
    													$open = $slot['opens'];
    													$open_time = strtotime( $open );
    													$open_display = date_i18n( $time_format, $open_time );
    													$open_His = date_i18n( 'H:i:s', $open_time );
    												}
    
    												if ( ! empty( $slot['closes'] ) ) {
    													$close = $slot['closes'];
    													$close_time = strtotime( $close );
    													$close_display = date_i18n( $time_format, $close_time );
    													$close_His = date_i18n( 'H:i:s', $close_time );
    												}
    										?>
    										<div class="gd-bh-hours<?php echo ( ( $open == '00:00' && $open == $close ) ? ' gd-bh-has24' : '' ); ?>">
    											<input type="text" id="<?php echo $unique_id; ?>_o" data-field-alt="open" data-bh="time" value="<?php echo esc_attr( $open_display ); ?>" aria-label="<?php esc_attr_e( 'Open', 'geodirectory' ); ?>" data-time="<?php echo $open_His; ?>" readonly> - <input type="text" id="<?php echo $unique_id; ?>_c" data-field-alt="close" data-bh="time" value="<?php echo esc_attr( $close_display ); ?>" aria-label="<?php esc_attr_e( 'Close', 'geodirectory' ); ?>" data-time="<?php echo $close_His; ?>" readonly><input type="hidden" id="<?php echo $unique_id; ?>_oa" name="<?php echo $htmlvar_name; ?>_f[hours][<?php echo $day_no; ?>][open][]" data-field="open" value="<?php echo esc_attr( $open ); ?>"><input type="hidden" id="<?php echo $unique_id; ?>_ca" name="<?php echo $htmlvar_name; ?>_f[hours][<?php echo $day_no; ?>][close][]" data-field="close" value="<?php echo esc_attr( $close ); ?>"> <span class="gd-bh-remove"><i class="fas fa-minus-circle" aria-hidden="true"></i></span>
    										</div>
    										<?php } ?>
    									<?php } else { ?>
    									<div class="gd-bh-closed"><?php _e( 'Closed', 'geodirectory' ); ?></div>
    									<?php } ?>
    								</td>
    								<td class="gd-bh-act"><span class="gd-bh-add"><i class="fas fa-plus-circle" aria-hidden="true"></i></span></td>
    							</tr>
    							<?php } ?>
    							<tr class="gd-tz-item">
    								<td colspan="4"><label form="<?php echo $htmlvar_name; ?>_f_timezone"><?php _e( 'Timezone offset from UTC (not including daylight savings time This is set automatically when address is set)', 'geodirectory' ); ?></label> 
                                        <input type="text" data-field="timezone" placeholder="<?php echo esc_attr( geodir_utc_offset_dst( $timezone_string ) ); ?>" id="<?php echo $htmlvar_name; ?>_f_timezone" value="<?php echo esc_attr( $gmt_offset ); ?>" lang="EN">
                                    </td>
                                </tr>
    						</tbody>
    					</table>
    				</div>
    				<input type="hidden" name="<?php echo $htmlvar_name; ?>" value="<?php echo esc_attr( $value ); ?>">
    			</div>
                <span class="geodir_message_note"><?php echo $description; ?></span>
            </div>
            <?php
            $html = ob_get_clean();
        }
    	
    	return $html;
    }

    Dear Stiofan,

    I have tried your snippet. It doesn’t seem to achieve the desired result. Please let me reiterate my idea.

    In Business Hours, on the admin settings side, I would like to have ‘Is Required’ option to check, with the custom required error message. Just as most other CFs have.

    The Business Hours front end, where the user fills in the New Listing form, both radio buttons should be unchecked as default, so the user has to select either Yes (in which case the options of days/hours appear), OR No, which means that no Business Hours data is needed and no data will be entered into database.

    I hope this makes it more clear.

    All best wishes,
    Eugene

    Also, I have changed the above mentioned core files back to their original state before trying your snippet. Was I right to do that?

    Plugin Author Stiofan

    (@stiofansisland)

    Orignal files should be set back, and the snippet if added right will automatically make the field required and work like you want, it does not add admin settings to the field, it just does it.

    Stiofan

    I see. I am using special mu-plugins file for all snippets related to GeoDirectory. I will try it again now. One thing – the snippet does not add the Red Asterisk by the label. But, I suppose it could be added within the snippet.
    I will reset the engine and delete all draft data before trying. Will let you know shortly.

    Nope. It still does not make it.
    The No radio option is set as selected by default.

    OK Stiofan, I will work around the original GeoDir code. I will just name the field as question “Set open hours?”.
    Also, I will have to get rid of the Timezone offset from UTC field from the source file as I don’t need it at all.
    Yet again, thank you very much for your help and input!
    All best,
    Eugene

    in input-functions.php I set the Timezone table row as hidden:
    <tr class="gd-tz-item" hidden>

    Plugin Author Stiofan

    (@stiofansisland)

    Make sure u are adding a new listing, this will always be set when editing a listing.

    The timezone is needed, u can hide it with CSS as in most cases it is set automatically.

    If you need more help with the snippet please post on our support forums noting FAO Stiofan. https://wpgeodirectory.com/support/

    Thanks,

    Stiofan

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Logic for Business Hours?’ is closed to new replies.