File: /var/www/html/TBbargain/wp-content/themes/Tbbargain/search.php
<?php /* Template Name: Product Details Page */
get_header();
?>
<section class="banner sub-bnr">
<div class="container custom-container">
<div class="txt">
<?php
// Ensure we're using the correct search query
if ( isset($_GET['s']) && !empty($_GET['s']) ) {
$search_term = sanitize_text_field($_GET['s']);
?>
<h2 class="title">Search Results for</h2>
<div class="breadcrumb">
<a class="nav-link" href="#"><?php echo esc_html($search_term) ?></a>
</div>
<?php } ?>
</div>
</div>
</section>
<section class="all-deal-wrap dtl-list">
<div class="container custom-container">
<div class="row search-results">
<?php
// Ensure we're using the correct search query
if ( isset($_GET['s']) && !empty($_GET['s']) ) {
$search_term = sanitize_text_field($_GET['s']);
// echo '<h2>Search Results for: ' . esc_html($search_term) . '</h2>';
}
// Default Query arguments to fetch products
$args = array(
'post_type' => 'custom_product',
'posts_per_page' => 8, // You can adjust this to show more posts
);
// If there's a search term, we add it to the query
if ( isset($search_term) && !empty($search_term) ) {
$args['s'] = $search_term; // Add search term to the query args
}
// Perform the custom product query
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
// Get custom fields
$deal_badge = get_field('deal_badge');
$carousel_images = get_field('carousel_images');
$logo_image = get_field('logo_image');
$subtitle = get_field('title');
$description = get_field('description');
$crossprice = get_field('crossprice');
$discounted_price = get_field('discounted_price');
$discount_percent = get_field('discount_percent');
$expired = get_field('expired');
$expired_value = maybe_unserialize($expired);
$deal_end_date = get_field('deal_end_date');
$image_ids = get_post_meta(get_the_ID(), '_product_image_ids', true);
$image_ids = $image_ids ? explode(',', $image_ids) : [];
$discount_percent = 0;
if ($crossprice > $discounted_price && $crossprice > 0) {
$discount_percent = round((($crossprice - $discounted_price) / $crossprice) * 100);
}
$post_date = get_the_date('Y-m-d'); // The date the post was created
$post_date_timestamp = strtotime($post_date); // Convert to timestamp for calculations
// Get the last day of the current week (Sunday)
$last_day_of_week = strtotime('next Sunday'); // Get next Sunday (this will give you the last day of the current week)
// Calculate the difference in days between the post date and the last day of the current week
$date_diff = floor(($last_day_of_week - $post_date_timestamp) / (60 * 60 * 24)); // Difference in days
?>
<div class="col-lg-3"><!-- card starts -->
<div class="card-row-wrap">
<div class="img-carouel-wrap">
<?php if (is_array($expired_value) && isset($expired_value[0]) && $expired_value[0] === 'Expired') {
?>
<div class="deal-badge hot">
<img src="<?php echo get_template_directory_uri(); ?>/img/deal-tag.svg" alt="" class="icon">
<div class="txt">Expired</div>
</div>
<?php } else {
if($date_diff<=7){
?>
<div class="deal-badge new">
<img src="<?php echo get_template_directory_uri(); ?>/img/deal-tag.svg" alt="" class="icon">
<div class="txt">New Deal</div>
</div>
<?php } } ?>
<?php if (!empty($image_ids)) : ?>
<div class="thumbImgSlider">
<?php foreach ($image_ids as $id) : ?>
<div>
<img src="<?php echo esc_url(wp_get_attachment_url($id)); ?>" alt="Product Image" style="max-width: 100%; height: auto;">
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<div class="content-wrap">
<?php if ($logo_image) : ?>
<img src="<?php echo esc_url($logo_image['url']); ?>" alt="" class="logo">
<?php endif; ?>
<a href="<?php echo get_permalink(); ?>" class="sub-title-link"><h2 class="sub-title"><?php echo esc_html($subtitle); ?></h2></a>
<div class="price">
<span class="large">$<?php echo esc_html($discounted_price); ?></span>
<span class="cross">$<?php echo esc_html($crossprice); ?></span>
</div>
<div class="offer">
<div class="off">
<img src="<?php echo get_template_directory_uri(); ?>/img/offer-white.svg" alt="">
<?php echo esc_html($discount_percent); ?>% Off
</div>
<div class="end"><?php $date_parts = explode('/', $deal_end_date); // Split the date by '/'
if (count($date_parts) === 3) {
$formatted_date = $date_parts[1] . '/' . $date_parts[0] . '/' . $date_parts[2]; // Rearrange to mm/dd/yyyy
echo esc_html($formatted_date);
} ?></div>
</div>
</div>
</div><!-- card ends -->
</div>
<?php
endwhile;
wp_reset_postdata();
else :
echo '<p>No product deals found.</p>';
endif;
?>
</div>
</div>
<div class="btn-wrap">
<!-- Add your 'View All' or 'Load More' button here if needed -->
</div>
</section>
<?php get_footer(); ?>