Skip to content

Instantly share code, notes, and snippets.

@metelidrissi
Last active March 27, 2020 04:26
Show Gist options
  • Save metelidrissi/9bd1627cb2947a951c764b8966150477 to your computer and use it in GitHub Desktop.
Save metelidrissi/9bd1627cb2947a951c764b8966150477 to your computer and use it in GitHub Desktop.
Creating a shortcode where you can show different filters
<?php
/**
** Shortcode [box_filter]
** Author: Met El Idrissi - https://www.metelidrissi.com
** Creates a box where you can filter by WooCommerce Categories.
** To use this shortcode follow this steps:
** 1 - Copy the entire copy and paste it inside your functions.pgp
** 2 - Open any page in your WordPress and paste the shortcode [box_filter]
** 3 - Remember to change the ID's for the respective ones you create on your WooCommerce Categories
**/
function box_shortcode_filter() {
$dropdown = "";
$args = array(
'selected' => 0,
'hierarchical' => 1,
'taxonomy' => 'product_cat',
'hide_empty' => false,
'exclude_tree' => 62,
'exclude' => 15, 62
);
$product_categories = get_terms( $args );
$dropdown .= "<select name='category'>";
$dropdown .= "<option value='categories'>Qualsevol categoría</option>";
foreach ($product_categories as $category) {
$dropdown .= "<option value = '" . esc_attr($category->slug) . "'>" . esc_html($category->name) . "</option>";
}
$dropdown .= "</select>";
/*
** Display attributes as a checkboxes.
** These attributes are categories inside a generic one.
** In this case we assume the special category with ID 62
*/
$checkbox = "";
$checkbox .= "<div class='row'>";
$checkbox .= "<div class='vc_column_container col-md-3'>" . woocommerce_subcats_from_parentcat_by_ID(63, 'Barrera') . "</div>";
$checkbox .= "<div class='vc_column_container col-md-3'>" . woocommerce_subcats_from_parentcat_by_ID(68, 'Sellado') . "</div>";
$checkbox .= "<div class='vc_column_container col-md-3'>" . woocommerce_subcats_from_parentcat_by_ID(72, 'Tratamiento') . "</div>";
$checkbox .= "<div class='vc_column_container col-md-3'>" . woocommerce_subcats_from_parentcat_by_ID(79, 'Propiedades mecánicas') . "</div>";
$checkbox .= "</div>";
$checkbox .= "<div class='row'>";
$checkbox .= "<div class='vc_column_container col-md-3'>" . woocommerce_subcats_from_parentcat_by_ID(85, 'Color') . "</div>";
$checkbox .= "<div class='vc_column_container col-md-3'>" . woocommerce_subcats_from_parentcat_by_ID(93, 'Otras características') . "</div>";
$checkbox .= "<div class='vc_column_container col-md-3'>&nbsp;</div>";
$checkbox .= "<div class='vc_column_container col-md-3'>&nbsp;</div>";
$checkbox .= "</div>";
//Display the submit button for the form
$submit = "";
$submit .= "<input type='submit'>";
$content = "";
$content .= "<form action='filter_redirect.php' method='get'>";
$content .= $dropdown . "<br><br><br>";
$content .= $checkbox . "<br>";
$content .= $submit;
$content .= "</form>";
return $content;
}
add_shortcode('box_filter', 'box_shortcode_filter');
/*
** Display subcategories by parent category ID.
*/
function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID, $title) {
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $parent_cat_ID,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
$checkbox = "";
$checkbox .= "<b>". $title . "</b></br>";
foreach ($subcats as $category) {
$checkbox .= "<input type='checkbox' name='attributes[]' value='".$category->slug."' />" . esc_html($category->name) . "<br>";
}
return $checkbox;
}
add_action('get_categories_by_id', 'woocommerce_subcats_from_parentcat_by_ID');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment