/wp-admin/plugins.php'; return; } global $wp_query; $total = $wp_query->found_posts; $context['totalproducts'] = $total; $context = Timber::get_context(); $context['sidebar'] = Timber::get_widgets( 'shop-sidebar' ); if ( is_singular( 'product' ) ) { $context['post'] = Timber::get_post(); $product = wc_get_product( $context['post']->ID ); $context['product'] = $product; $context['price'] = $product->get_price_html(); $context['price_number'] = $product->get_price(); $context['gallery_ids'] = $product->get_gallery_image_ids(); if ( get_field('delivery_option') != 'custom' ) { $context['delivery_option'] = get_field(get_field('delivery_option'), 'option'); } else { $context['delivery_option'] = get_field('custom_delivery_message'); } // Get related products $related_limit = wc_get_loop_prop( 'columns' ); $related_ids = get_field('custom_related_products', $context['post']->id, $related_limit ); if ($related_ids) { $context['custom_related_products'] = Timber::get_posts( $related_ids ); } else { $primary_cat_id = get_post_meta($product->get_id(), '_yoast_wpseo_primary_' . 'product_cat', true); if($primary_cat_id){ $primary_cat = get_term($primary_cat_id, 'product_cat'); } $args = array( 'post_type' => 'product', 'posts_per_page' => 4 ); if(isset($primary_cat)){ $args['tax_query'] = array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $primary_cat, ) ); } if (Timber::get_posts($args)) { $context['related_products'] = Timber::get_posts($args); } } if (isset($_GET['added']) && $_GET['added'] == 1) { $context['added'] = true; } $context['hint_modal'] = true; // Restore the context and loop back to the main query loop. wp_reset_postdata(); Timber::render( 'views/woo/single-product.twig', $context ); } else { if(isset($_GET['minPrice']) || isset($_GET['maxPrice'])){ woocommerce_reset_loop(); $min = isset($_GET['minPrice']) ? $_GET['minPrice'] : ''; $max = isset($_GET['maxPrice']) ? $_GET['maxPrice'] : ''; $args = array( 'post_status' => 'publish', 'post_type' => 'product', 'posts_per_page' => -1 ); if($min || $max){ $args['meta_query'] = array( array( 'key' => '_price', 'value' => array($min, $max), 'compare' => 'BETWEEN', 'type' => 'NUMERIC' ) ); } $context['min'] = $min; $context['max'] = $max; $context['products'] = Timber::get_posts(new WP_Query($args)); } else { if(isset($context['query_strings']) && sizeof($context['query_strings']) > 0){ $queried_object = get_queried_object(); $args = array( 'post_status' => 'publish', 'post_type' => 'product', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => '_stock_status', 'value' => 'instock', 'compare' => '=', ) ) ); $tax_query = array( 'relation' => 'AND' ); if (isset($_GET['orderby'])) { $current_order = $_GET['orderby']; switch ( $current_order ) { case 'date' : $orderby = 'date'; $order = 'desc'; $meta_key = ''; break; case 'price' : $orderby = 'meta_value_num'; $order = 'asc'; $meta_key = '_price'; break; case 'price-desc' : $orderby = 'meta_value_num'; $order = 'desc'; $meta_key = '_price'; break; default : $orderby = 'menu_order title'; $order = 'asc'; $meta_key = ''; break; } $args['orderby'] = $orderby; $args['order'] = $order; if ($meta_key) : $args['meta_key'] = $meta_key; endif; } foreach ($context['query_strings'] as $key => $string) { if($key == 'q'){ } else { if ($key == 'orderby' || $key == 'paged') { continue; } $terms = explode(',', $string); $arr = array( 'taxonomy' => $key, 'field' => 'slug', 'operator' => 'IN', 'terms' => $terms ); array_push($tax_query, $arr); } } if(isset($queried_object->slug)){ $cat = array( 'taxonomy' => 'product_cat', 'terms' => array($queried_object->slug), 'field' => 'slug', 'operator' => 'IN' ); array_push($tax_query, $cat); } $args['tax_query'] = $tax_query; $posts = Timber::get_posts(new WP_Query($args)); } else { $posts = Timber::get_posts(); } $context['products'] = $posts; } if ( is_product_category() ) { $queried_object = get_queried_object(); $term_id = $queried_object->term_id; $context['category'] = new TimberTerm(get_term( $term_id, 'product_cat')); $args = array( 'taxonomy' => 'product_cat', 'hide_empty' => true, 'parent' => $term_id // 347 is the term ID for Jewllery, for reference. ); $exploded = explode('/', $_SERVER['REQUEST_URI']); if(isset($exploded[4])){ // we are too deep to borher with price filters... $context['deep_filter'] = true; } $context['categories'] = get_terms($args); // drop your attributes you want to filter on here. $attributes = array('pa_design', 'pa_diamondshape'); foreach ($attributes as $i => $tax) { // use some default args for all, regardless of exlusions $args = array( 'taxonomy' => $tax, 'hide_empty' => false ); // if there is some query params to filter on, lets use em! if(isset($context['query_strings'][$tax])){ $exclusions = explode(',', $context['query_strings'][$tax]); foreach ($exclusions as $key => $value) { $excludeIDs[$key] = get_term_by('slug', $value, $tax)->term_id; } // add the exlusions to the already in place args for the get_terms coming up $args['exclude'] = $excludeIDs; } $context[$tax] = get_terms($args); } $context['title'] = single_term_title( '', false ); } if (is_search()) { global $wpdb; // Retrieve the search term and 'orderby' parameter from the GET request $search_term = isset($_GET['s']) ? $_GET['s'] : ''; $orderby = isset($_GET['orderby']) ? $_GET['orderby'] : 'post_date'; // Define allowed orderby values for security $allowed_orderby = array('post_date', 'post_title', 'price', 'sku'); if (!in_array($orderby, $allowed_orderby)) { $orderby = 'post_date'; // Default to 'post_date' if invalid value is provided } // Construct the raw SQL query $sql = " SELECT DISTINCT p.ID FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} pm1 ON p.ID = pm1.post_id AND pm1.meta_key = '_sku' LEFT JOIN {$wpdb->postmeta} pm2 ON p.ID = pm2.post_id AND pm2.meta_key = '_price' WHERE p.post_type = 'product' AND p.post_status = 'publish' AND ( pm1.meta_value LIKE %s OR p.post_title LIKE %s OR p.post_content LIKE %s ) "; // Determine the orderby clause switch ($orderby) { case 'price': $sql .= " ORDER BY CAST(pm2.meta_value AS UNSIGNED)"; // Order by price break; case 'sku': $sql .= " ORDER BY pm1.meta_value"; // Order by SKU break; case 'post_title': $sql .= " ORDER BY p.post_title"; // Order by post title break; case 'post_date': default: $sql .= " ORDER BY p.post_date"; // Order by post date (default) break; } // Prepare the query $sql = $wpdb->prepare($sql, '%' . $wpdb->esc_like($search_term) . '%', '%' . $wpdb->esc_like($search_term) . '%', '%' . $wpdb->esc_like($search_term) . '%'); // Execute the query $product_ids = $wpdb->get_col($sql); // Retrieve the product posts using Timber $context['products'] = Timber::get_posts(array( 'post_type' => 'product', 'post__in' => $product_ids, 'orderby' => 'post__in', // Preserve the order of IDs as returned by the SQL query 'posts_per_page' => -1, )); $context['search_term'] = $search_term; // Render the template Timber::render('views/woo/search-results.twig', $context); } else { if (isset($context['category'])) { if ($context['category']->taxonomy == 'product_cat') { // new category page $context['category'] = new TimberTerm(get_term( $term_id, 'product_cat')); $context['meta_title'] = get_term_meta($term_id, 'wpseo_title', true); $shop_link = explode('?', $_SERVER['REQUEST_URI'], 2)[0]; $context['shop_link'] = substr($shop_link, -1) !== '/' ? $shop_link . '/' : $shop_link; if (sizeof($context['products']) == 0) { $context['no_results'] = true; } if ($context['category']->has_landing_page || $term_id == 1061 || $term_id == 998) { // online exclusive or landing page toggled switch ($context['category']->name) { case 'Online Exclusive': $attributes = array( 'pa_gem-stone', 'pa_ring-style', 'pa_metal', ); break; case 'Hot Right Now': $attributes = array( 'pa_jewellery-category', 'pa_gem-stone', 'pa_ring-style', 'pa_metal', 'pa_length', 'pa_finger-size', 'pa_value', 'pa_earring-style', 'pa_earring-fitting', 'pa_gem-stone-cut' ); // Define allowed term slugs or IDs for "Hot Right Now" $allowed_terms = array( 'pa_jewellery-category' => array('earrings', 'ring','pendant'), 'pa_gem-stone' => array('diamond','lab-grown-diamond','cz','emerald','amethyst', 'blue-topaz'), 'pa_ring-style' => array('solitaire','trilogy','half-hoop','eternity-ring','wedding-ring','engagement','halo','cluster-ring'), 'pa_metal' => array('platinum','silver','sterling-silver','white-gold','yellow-gold'), 'pa_length' => array('16-inch','18-inch','16-18-inches'), 'pa_value' => array('0-100','100-200','200-300','400-500','500-600','600-700','700-800','800-900','900-1000','1000-plus'), 'pa_earring-style' => array('stud','hoop','knot','drop'), 'pa_earring-fitting' => array('hinged','post-and-butterfly','hinge-and-post'), 'pa_finger-size' => array('sizeable','n-leading-edge','m-leading-edge','multiple'), 'pa_gem-stone-cut' => array('round-brilliant-cut','emerald-cut','marquise-cut','oval-cut','pear-cut','princess-cut'), ); break; case 'Rings': $attributes = array( 'pa_gem-stone', 'pa_ring-style', 'pa_metal', ); break; case 'Necklaces': case 'Pendants': $attributes = array( 'pa_gem-stone', 'pa_length', 'pa_metal', ); break; case 'Gold Jewellery': $attributes = array( 'pa_jewellery-category', 'pa_length', 'pa_metal', ); break; case 'Diamond Jewellery': $attributes = array( 'pa_jewellery-category', 'pa_gem-stone', 'pa_metal', ); break; case 'Earrings': case 'Bracelets': $attributes = array( 'pa_gem-stone', 'pa_metal', ); break; case 'Engagement Rings': $attributes = array( 'pa_metal', 'pa_ring-style', 'pa_engagement-ring-design', 'pa_gem-stone', 'pa_gem-stone-cut' ); break; case 'Wedding Rings': $attributes = array( 'pa_metal', 'pa_gem-stone', 'pa_wedding-ring-design', 'pa_gem-stone-cut', 'pa_setting-style' ); break; case 'Eternity Rings': $attributes = array( 'pa_metal', 'pa_gem-stone', 'pa_eternity-ring-design', 'pa_gem-stone-cut', 'pa_setting-style' ); break; case 'Engagement & Wedding': $attributes = array( 'pa_metal', 'pa_gem-stone', 'pa_ring-style', 'pa_gem-stone-cut', 'pa_setting-style' ); break; case 'CN Jewellery': $attributes = array( 'pa_jewellery-category', 'pa_gem-stone', 'pa_metal' ); break; case 'For Her': $attributes = array( 'pa_jewellery-category', 'pa_metal', 'pa_gem-stone', ); break; case 'For Him': $attributes = array( 'pa_jewellery-category', 'pa_metal', ); break; case 'Jewellery Sale': $attributes = array( 'pa_jewellery-category', 'pa_gem-stone', 'pa_metal', 'pa_finger-size' ); break; case 'CN Silver Collection': $attributes = array( 'pa_jewellery-category', 'pa_gem-stone' ); break; case 'Pre-Owned Jewellery': $attributes = array( 'pa_brand', 'pa_jewellery-category', 'pa_gem-stone', 'pa_metal', ); break; case 'CN Lab Grown Diamond Collection': $attributes = array( 'pa_jewellery-category', 'pa_gem-stone-cut', 'pa_metal', ); break; case 'Brands': $attributes = array( 'pa_jewellery-category', 'pa_gem-stone-cut', 'pa_metal', ); break; default: // code... break; } foreach ($attributes as $i => $tax) { // use some default args for all, regardless of exclusions $args = array( 'taxonomy' => $tax, 'hide_empty' => true ); // if there are some query params to filter on, let's use them! if (isset($context['query_strings'][$tax])) { $exclusions = explode(',', $context['query_strings'][$tax]); $excludeIDs = array(); // Initialize the array to store term IDs foreach ($exclusions as $key => $value) { $term = get_term_by('slug', $value, $tax); if ($term) { $excludeIDs[] = $term->term_id; } } // Add the exclusions to the context $context['exclusions'][$tax] = $excludeIDs; } $terms = get_terms($args); // Filter terms if category is "Hot Right Now" if ($context['category']->name == 'Hot Right Now') { $filtered_terms = array(); foreach ($terms as $term) { if (in_array($term->slug, $allowed_terms[$tax])) { $filtered_terms[] = $term; } } $terms = $filtered_terms; } $attribute_label_name = wc_attribute_label($tax); $context['attributes'][$tax]['attributes'] = $terms; $context['attributes'][$tax]['name'] = $attribute_label_name; $context['attributes'][$tax]['filter_type'] = $tax; } //if($term_id == 1061){ if($term_id == 998 ){ // temp redirect for now //wp_redirect('/jewellery', 302); //exit; //$template = 'views/woo/online-exclusive.twig'; $template = 'views/woo/hot-right-now.twig'; } elseif($term_id == 1061){ $template = 'views/woo/hot-right-now.twig'; } else { $template = 'views/woo/archive-2024.twig'; } Timber::render( $template, $context ); } else { Timber::render( $template, $context ); } } } } }