Last active
May 7, 2022 18:46
-
-
Save jmarreros/01ebe8b0446d72f17d3e037d3b0ba942 to your computer and use it in GitHub Desktop.
Filtros personalizados de marca y stock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Set query vars | |
add_filter('query_vars', 'dcms_add_query_vars'); | |
function dcms_add_query_vars($query_vars) { | |
$query_vars[] = "dcms-stock"; | |
$query_vars[] = "dcms-brand"; | |
return $query_vars; | |
} | |
// Modify main query | |
add_filter('posts_clauses', 'dcms_filter_query_categories'); | |
function dcms_filter_query_categories($posts_clauses) { | |
global $wpdb; | |
if ( ! is_admin() && is_woocommerce() && (is_product_category() || is_product_tag())) { | |
$stock = get_query_var('dcms-stock'); | |
$brand = get_query_var('dcms-brand'); | |
// If brand is selected | |
if ( $brand ){ | |
$posts_clauses['join'] .= " INNER JOIN {$wpdb->prefix}term_relationships trs ON ( trs.object_id = $wpdb->posts.ID) | |
INNER JOIN {$wpdb->prefix}term_taxonomy tt ON (trs.term_taxonomy_id = tt.term_taxonomy_id)"; | |
$posts_clauses['where'] .= " AND tt.term_id = $brand "; | |
} | |
// If stock is selected | |
if ( $stock ){ | |
$posts_clauses['join'] .= " INNER JOIN {$wpdb->prefix}postmeta spm ON ( spm.post_id = $wpdb->posts.ID) "; | |
if ( $stock === 'stock'){ | |
$posts_clauses['where'] .= " AND spm.meta_key = '_stock_status' AND spm.meta_value = 'instock'"; | |
} else if ( $stock === 'no-stock'){ | |
$posts_clauses['where'] .= " AND spm.meta_key = '_stock_status' AND spm.meta_value = 'outofstock'"; | |
} | |
} | |
} | |
return $posts_clauses; | |
} | |
// Process filter forms | |
add_action( 'admin_post_nopriv_process_dcms_filter', 'dcms_process_filter' ); | |
add_action( 'admin_post_process_dcms_filter', 'dcms_process_filter' ); | |
function dcms_process_filter(){ | |
$stock = $_POST['dcms-filter-stock']??''; | |
$brand = $_POST['dcms-filter-brands']??''; | |
$url = $_POST['current_url']??''; | |
wp_redirect( add_query_arg(['dcms-stock' => $stock, 'dcms-brand' => $brand], $url)); | |
} | |
// Build filter Html form | |
add_action('woocommerce_archive_description', 'dcms_show_filters'); | |
function dcms_show_filters(){ | |
global $wp; | |
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) ); | |
if ( is_product_category() ){ | |
echo "<form class='dcms-filters' method='post' action='".admin_url( 'admin-post.php' )."'>"; | |
dcms_show_filter_brand(); | |
dcms_show_filter_stock(); | |
echo "<input type='hidden' name='current_url' value='{$current_url}'>"; | |
echo "<input type='hidden' name='action' value='process_dcms_filter'>"; | |
echo "<input type='submit' name='submit' value='Filtrar'>"; | |
echo "</form>"; | |
} | |
} | |
// Html filter stock | |
function dcms_show_filter_stock(){ | |
$stock_var = get_query_var('dcms-stock'); | |
echo "<label for='dcms-filter-stock'>Stock: </label>"; | |
echo "<select class='dcms-filter-stock' name='dcms-filter-stock'>"; | |
echo "<option value=''>Todos</option>"; | |
echo "<option value='stock' " . ( $stock_var=='stock'?'selected':'') . ">Sólo existencias</option>"; | |
echo "<option value='no-stock' " . ( $stock_var=='no-stock'?'selected':'') . ">Sin stock</option>"; | |
echo "</select>"; | |
} | |
// Html filter brand | |
function dcms_show_filter_brand(){ | |
$category_id = get_queried_object_id(); | |
$brands = dcms_get_brands_category($category_id); | |
$brand_var = get_query_var('dcms-brand'); | |
if ( count($brands) > 0){ | |
echo "<label for='dcms-filter-brands'>Marcas: </label>"; | |
echo "<select class='dcms-filter-brands' name='dcms-filter-brands'>"; | |
echo "<option value=''>Todos</option>"; | |
foreach ($brands as $brand) { | |
$selected = $brand_var == $brand->term_id ? 'selected' : ''; | |
echo "<option value='{$brand->term_id}' {$selected}>{$brand->name}</option>"; | |
} | |
echo "</select>"; | |
} | |
} | |
// DB Query brands currenty category | |
function dcms_get_brands_category( $category_id ){ | |
global $wpdb; | |
$sql = "SELECT DISTINCT t.term_id, t.name | |
FROM {$wpdb->prefix}posts p | |
INNER JOIN {$wpdb->prefix}term_relationships prs ON p.ID = prs.object_id | |
INNER JOIN {$wpdb->prefix}term_relationships prsb ON p.ID = prsb.object_id | |
INNER JOIN {$wpdb->prefix}term_taxonomy tt ON prsb.term_taxonomy_id = tt.term_taxonomy_id | |
INNER JOIN {$wpdb->prefix}terms t ON tt.term_id = t.term_id | |
WHERE | |
p.post_status = 'publish' | |
AND prs.term_taxonomy_id = {$category_id} | |
AND tt.taxonomy = 'product_brand'"; | |
return $wpdb->get_results($sql); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment