Hi @andrii,
Thanks for your reply!
I’ve finally had a chance to play around with this idea and seem to have things working on my end.
Snippet for Available/Unavailable status:
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_style( 'font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css' );
} );
add_filter( 'hivepress/v1/templates/listing_view_block', function( $template ) {
// Function to get status HTML
$get_status_html = function( $listing ) {
if ( ! is_a( $listing, 'HivePress\Models\Listing' ) ) {
error_log( 'Invalid listing object in get_status_html for listing ID: ' . ( $listing ? $listing->get_id() : 'unknown' ) );
return '';
}
$listing_id = $listing->get_id();
$opening_hours = $listing->get_opening_hours();
$is_24_7 = $listing->is_always_open();
error_log( 'Opening Hours for Listing ' . $listing_id . ': ' . print_r( $opening_hours, true ) );
error_log( 'Listing ' . $listing_id . ' is_24_7: ' . ( $is_24_7 ? 'true' : 'false' ) );
// If 24/7 is set, show available
if ( $is_24_7 ) {
error_log( 'Listing ' . $listing_id . ' detected as 24/7 open' );
return '<span class="hp-listing__status status-icon available"><i class="fas fa-check-circle"></i> Available</span>';
}
// If no opening hours or not an array, hide the status
if ( empty( $opening_hours ) || ! is_array( $opening_hours ) ) {
error_log( 'No valid opening hours for Listing ' . $listing_id . ', hiding status' );
return '';
}
// Get current day and time (BST, respecting WordPress timezone)
$current_day = wp_date( 'l', current_time( 'timestamp' ) ); // e.g., 'Sunday'
$current_time = wp_date( 'H:i', current_time( 'timestamp' ) ); // e.g., '15:32'
// Check if current day exists in opening hours
if ( ! isset( $opening_hours[ $current_day ] ) || empty( $opening_hours[ $current_day ] ) ) {
error_log( 'No hours set for ' . $current_day . ' in Listing ' . $listing_id );
return '<span class="hp-listing__status status-icon unavailable"><i class="fas fa-times-circle"></i> Unavailable</span>';
}
// Get open and close times
$open_time = $opening_hours[ $current_day ]['open'] ?? '';
$close_time = $opening_hours[ $current_day ]['close'] ?? '';
error_log( 'Listing ' . $listing_id . ' on ' . $current_day . ': open=' . $open_time . ', close=' . $close_time . ', current=' . $current_time );
// Validate times
if ( empty( $open_time ) || empty( $close_time ) ) {
error_log( 'Invalid open/close times for Listing ' . $listing_id );
return '<span class="hp-listing__status status-icon unavailable"><i class="fas fa-times-circle"></i> Unavailable</span>';
}
// Handle 24/7 case (e.g., open and close are 00:00 or 23:59)
if ( $open_time === '00:00' && ( $close_time === '00:00' || $close_time === '23:59' ) ) {
error_log( 'Listing ' . $listing_id . ' detected as 24/7 open (time-based)' );
return '<span class="hp-listing__status status-icon available"><i class="fas fa-check-circle"></i> Available</span>';
}
// Compare times
$is_open = $current_time >= $open_time && $current_time < $close_time;
error_log( 'Listing ' . $listing_id . ' is_open=' . ( $is_open ? 'true' : 'false' ) );
if ( $is_open ) {
return '<span class="hp-listing__status status-icon available"><i class="fas fa-check-circle"></i> Available</span>';
} else {
return '<span class="hp-listing__status status-icon unavailable"><i class="fas fa-times-circle"></i> Unavailable</span>';
}
};
// Get the listing from the template context
$listing = $template['context']['listing'] ?? null;
$status_html = '';
if ( $listing && is_a( $listing, 'HivePress\Models\Listing' ) ) {
$status_html = $get_status_html( $listing );
} else {
error_log( 'Listing context missing or invalid in listing_view_block filter' );
}
// Add status block to the primary area
return hivepress()->helper->merge_trees(
$template,
[
'blocks' => [
'listing_actions_primary' => [
'blocks' => [
'listing_status' => [
'type' => 'content',
'content' => $status_html, // Pre-rendered string
'_order' => 50,
],
],
],
],
]
);
} );
Alternate Snippet for Open/Closed text status:
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_style( 'font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css' );
} );
add_filter( 'hivepress/v1/templates/listing_view_block', function( $template ) {
// Function to get status HTML
$get_status_html = function( $listing ) {
if ( ! is_a( $listing, 'HivePress\Models\Listing' ) ) {
error_log( 'Invalid listing object in get_status_html for listing ID: ' . ( $listing ? $listing->get_id() : 'unknown' ) );
return '';
}
$listing_id = $listing->get_id();
$opening_hours = $listing->get_opening_hours();
$is_24_7 = $listing->is_always_open();
error_log( 'Opening Hours for Listing ' . $listing_id . ': ' . print_r( $opening_hours, true ) );
error_log( 'Listing ' . $listing_id . ' is_24_7: ' . ( $is_24_7 ? 'true' : 'false' ) );
// If 24/7 is set, show open
if ( $is_24_7 ) {
error_log( 'Listing ' . $listing_id . ' detected as 24/7 open' );
return '<span class="hp-listing__status status-icon open"><i class="fas fa-check-circle"></i> Open</span>';
}
// If no opening hours or not an array, hide the status
if ( empty( $opening_hours ) || ! is_array( $opening_hours ) ) {
error_log( 'No valid opening hours for Listing ' . $listing_id . ', hiding status' );
return '';
}
// Get current day and time (BST, respecting WordPress timezone)
$current_day = wp_date( 'l', current_time( 'timestamp' ) ); // e.g., 'Sunday'
$current_time = wp_date( 'H:i', current_time( 'timestamp' ) ); // e.g., '15:32'
// Check if current day exists in opening hours
if ( ! isset( $opening_hours[ $current_day ] ) || empty( $opening_hours[ $current_day ] ) ) {
error_log( 'No hours set for ' . $current_day . ' in Listing ' . $listing_id );
return '<span class="hp-listing__status status-icon closed"><i class="fas fa-times-circle"></i> Closed</span>';
}
// Get open and close times
$open_time = $opening_hours[ $current_day ]['open'] ?? '';
$close_time = $opening_hours[ $current_day ]['close'] ?? '';
error_log( 'Listing ' . $listing_id . ' on ' . $current_day . ': open=' . $open_time . ', close=' . $close_time . ', current=' . $current_time );
// Validate times
if ( empty( $open_time ) || empty( $close_time ) ) {
error_log( 'Invalid open/close times for Listing ' . $listing_id );
return '<span class="hp-listing__status status-icon closed"><i class="fas fa-times-circle"></i> Closed</span>';
}
// Handle 24/7 case (e.g., open and close are 00:00 or 23:59)
if ( $open_time === '00:00' && ( $close_time === '00:00' || $close_time === '23:59' ) ) {
error_log( 'Listing ' . $listing_id . ' detected as 24/7 open (time-based)' );
return '<span class="hp-listing__status status-icon open"><i class="fas fa-check-circle"></i> Open</span>';
}
// Compare times
$is_open = $current_time >= $open_time && $current_time < $close_time;
error_log( 'Listing ' . $listing_id . ' is_open=' . ( $is_open ? 'true' : 'false' ) );
if ( $is_open ) {
return '<span class="hp-listing__status status-icon open"><i class="fas fa-check-circle"></i> Open</span>';
} else {
return '<span class="hp-listing__status status-icon closed"><i class="fas fa-times-circle"></i> Closed</span>';
}
};
// Get the listing from the template context
$listing = $template['context']['listing'] ?? null;
$status_html = '';
if ( $listing && is_a( $listing, 'HivePress\Models\Listing' ) ) {
$status_html = $get_status_html( $listing );
} else {
error_log( 'Listing context missing or invalid in listing_view_block filter' );
}
// Add status block to the primary area
return hivepress()->helper->merge_trees(
$template,
[
'blocks' => [
'listing_actions_primary' => [
'blocks' => [
'listing_status' => [
'type' => 'content',
'content' => $status_html, // Pre-rendered string
'_order' => 50, // Before reply/favorite icons (adjust to 10-20 for after)
],
],
],
],
]
);
} );
CSS:
/* Style the status text and icon */
.hp-listing__status.status-icon.available {
color: green;
font-size: 14px;
display: inline-flex;
align-items: center;
margin-left: 155px;
}
/* Style the unavailable status */
.hp-listing__status.status-icon.unavailable {
color: red;
font-size: 14px;
display: inline-flex;
align-items: center;
margin-left: 155px;
}
/* Responsive adjustments for tablets */
@media (max-width: 768px) {
.hp-listing__status.status-icon.available,
.hp-listing__status.status-icon.unavailable {
margin-left: 440px;
font-size: 13px;
}
}
/* Responsive adjustments for smaller screens */
@media (max-width: 450px) {
.hp-listing__status.status-icon.available,
.hp-listing__status.status-icon.unavailable {
margin-left: 100px;
font-size: 12px;
}
}
/* Space between icon and text in status */
.hp-listing__status i {
margin-right: 5px; /* Adjust spacing between icon and text */
}
Result:
Shows an Available / Unavailable status where applicable, and hides the status if no opening hours are set.



Cheers,
Chris 