File: /var/www/html/TBbargain/wp-content/themes/Tbbargain/taxonomy-product_category.php
<?php /* Template Name: Product List Page */
get_header();
$term = get_queried_object();
$current_url = $_SERVER["REQUEST_URI"];
$category_slug = basename($current_url);
$term = get_queried_object();
// Get the category object based on the slug
$category = get_category_by_slug($category_slug);
?>
<section class="banner sub-bnr">
<div class="container custom-container">
<div class="txt">
<h2 class="title">Top In <?php echo ucfirst($term->slug); ?></h2>
<div class="breadcrumb">
<a class="nav-link" href="#">Home</a>
<a class="nav-link" href="#"><?php echo ucfirst($term->slug); ?></a>
<!-- <a class="nav-link" href="#">Mobiles</a> -->
</div>
</div>
</div>
</section>
<section class="listing">
<div class="container custom-container">
<div class="listing-wrapper">
<div class="sidebar">
<div class="title">Filters</div>
<!-- <div class="showing-blk">Showing 1 – 24 Of 10,455 Results For "Mobile"</div> -->
<div class="box-blks">
<div class="box-title">
Categories
</div>
<?php
$parent_category = get_term_by('slug', 'deal-category', 'product_category');
$categories = get_terms(array(
'taxonomy' => 'product_category', // Custom taxonomy name
'hide_empty' => false, // Hide categories with no posts
'parent' => $parent_category->term_id,
));
$current_category = isset($_GET['category']) ? sanitize_text_field($_GET['category']) : '';
if (!empty($categories) && !is_wp_error($categories)) : ?>
<ul class="filter-listing">
<?php foreach ($categories as $category) : ?>
<li>
<a class="f-link <?php echo ($current_category === $category->slug) ? 'active' : ''; ?>" href="?category=<?php echo esc_attr($category->slug); ?>">
<?php echo esc_html($category->name); ?>
</a>
<span><?php echo esc_html($category->count); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<p>No categories found.</p>
<?php endif; ?>
</div>
<div class="box-blks">
<div class="box-title">
Offer Percentage
</div>
<?php
// Define the percentage ranges
$percentage_ranges = array(
'0-20%' => '0-20',
'21-40%' => '21-40',
'41-60%' => '41-60',
'61-80%' => '61-80',
'81-100%' => '81-100',
);
$percentage_range = isset($_GET['percentage_range']) ? sanitize_text_field($_GET['percentage_range']) : '';
// Loop through the ranges to display them in the list
?>
<ul class="filter-listing">
<?php foreach ($percentage_ranges as $label => $range) : ?>
<li>
<a class="f-link <?php echo ($percentage_range === $range) ? 'active' : ''; ?>" href="?percentage_range=<?php echo esc_attr($range); ?>">
<?php echo esc_html($label); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<div class="box-blks">
<div class="box-title">
Vendors
</div>
<?php
$vendor_category = get_term_by('slug', 'vendors', 'product_category');
$vendors = get_terms(array(
'taxonomy' => 'product_category', // Custom taxonomy name
'hide_empty' => false, // Hide categories with no posts
'parent' => $vendor_category->term_id,
));
$current_vendor = isset($_GET['vendor']) ? sanitize_text_field($_GET['vendor']) : '';
if (!empty($vendors) && !is_wp_error($vendors)) : ?>
<ul class="filter-listing">
<?php foreach ($vendors as $vendor) : ?>
<li>
<a class="f-link <?php echo ($current_vendor === $vendor->slug) ? 'active' : ''; ?>" href="?vendor=<?php echo esc_attr($vendor->slug); ?>">
<?php echo esc_html($vendor->name); ?>
</a>
<span> <?php
$args = array(
'post_type' => 'custom_product', // Custom post type
'posts_per_page' => -1, // Show all posts
'meta_query' => array(
array(
'key' => 'vendors', // Custom field for vendor
'value' => $vendor->name, // The vendor ID to match
'compare' => '=', // Exact match
),
),
);
// Query the posts
$query = new WP_Query($args);
// Output the count of matching posts
echo esc_html($query->found_posts);
// Reset the global post data after the query
wp_reset_postdata();
?></span>
</li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<p>No categories found.</p>
<?php endif; ?>
</div>
</div>
<div class="right-listing">
<!-- <div class="select-blk">
<span class="sort">Sort by : </span>
<select class="form-select" aria-label="Default select example">
<option selected>Featured</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
</div> -->
<div class="card-wrp no-data">
<?php
$term = get_queried_object();
$category_slug = isset($_GET['category']) ? sanitize_text_field($_GET['category']) : '';
$percentage_range = isset($_GET['percentage_range']) ? sanitize_text_field($_GET['percentage_range']) : '';
// print_r($term);
if(isset($_GET['category']))
{
$parent_category = get_term_by('slug', 'deal-category', 'product_category');
$args = array(
'post_type' => 'custom_product', // Custom post type
'posts_per_page' => -1, // Show all posts
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy, // Current taxonomy
'field' => 'slug', // Match by slug
'terms' => $category_slug, // Current term slug
),
),
);
}
else if(isset($_GET['percentage_range']))
{
$percentage_range = sanitize_text_field($_GET['percentage_range']);
$range = explode('-', $percentage_range);
// Ensure the range has two valid numeric values
if (count($range) === 2 && is_numeric($range[0]) && is_numeric($range[1])) {
$min_percentage = (int)$range[0];
$max_percentage = (int)$range[1];
$args = array(
'post_type' => 'custom_product', // Custom post type
'posts_per_page' => -1, // Show all posts
'meta_query' => array(
array(
'key' => 'discount_percent', // Custom field for discount percent
'value' => array($min_percentage, $max_percentage), // Min and max values
'compare' => 'BETWEEN', // Check if value is within the range
'type' => 'NUMERIC', // Ensure comparison is numeric
),
),
);
}
}
else if(isset($_GET['vendor']))
{
$vendor_id = sanitize_text_field($_GET['vendor']);
$args = array(
'post_type' => 'custom_product', // Custom post type
'posts_per_page' => -1, // Show all posts
'meta_query' => array(
array(
'key' => 'vendors', // Custom field for discount percent
'value' => $vendor_id, // Min and max values
'compare' => '=', // Ensure comparison is numeric
),
),
);
}
else if(isset($_GET['all']))
{
$args = array(
'post_type' => 'custom_product', // Custom post type
'posts_per_page' => -1, // Show all posts
);
}
else
{
$args = array(
'post_type' => 'custom_product', // Custom post type
'posts_per_page' => 1, // Show all posts
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy, // Current taxonomy
'field' => 'slug', // Match by slug
'terms' => $term->slug, // Current term slug
),
),
);
}
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
// Custom fields
$deal_badge = get_field('deal_badge');
$carousel_images = get_field('carousel_images');
//------------------------------------------------------------------
$vendors = get_field('vendors');
$vendor_term = get_term_by('name', $vendors, 'product_category');
if ($vendor_term) {
$category_id = $vendor_term->term_id; // Get the category ID
$category_icon = get_term_meta($category_id, '_category_icon', true);
}
//-----------------------------------------------------------------------
$title = get_field('title');
$description = get_field('description');
$crossprice = get_field('crossprice');
$discounted_price = get_field('discounted_price');
$discount_percent = get_field('discount_percent');
$deal_end_date = get_field('deal_end_date');
$expired = get_field('expired');
$expired_value = maybe_unserialize($expired);
$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
?>
<a href="<?php echo get_permalink(); ?>" class="card-row-wrap" data-post-id="<?php echo get_the_ID(); ?>">
<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 ($vendor_term) { if ($category_icon) : ?>
<img src="<?php echo esc_url($category_icon); ?>" alt="" class="logo">
<?php endif; } ?>
<h2 class="sub-title"><?php echo esc_html($title); ?></h2>
<!-- <p class="sub-para"><?php echo esc_html($description); ?></p> -->
<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>
<button class="btn-link" href="#" fdprocessedid="lucfkl">Go to the page</button>
</div>
</a>
<?php
endwhile;
wp_reset_postdata();
else : ?>
<!-- <div class="no-data"> -->
<?php echo '<p>No product deals found.</p>';?>
<!-- </div> -->
<?php endif;
?>
</div>
<?php if ($query->have_posts()) : ?>
<div class="btn-blk">
<!-- <a href="#" class="view-all" id="load-more">View All</a> -->
<button class="view-all" id="load-more">View All</button>
</div>
<?php endif; ?>
</div>
</div>
</div>
</section>
<?php get_footer(); ?>