Skip to content

Instantly share code, notes, and snippets.

@codehooligans
Created July 1, 2012 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codehooligans/3028498 to your computer and use it in GitHub Desktop.
Save codehooligans/3028498 to your computer and use it in GitHub Desktop.
MP Category Exclude
/* Exclude logic for MarketPress Product Categories */
// Add product_categories to exclude here. Note this is an array so multiple category IDs need to be seperated by comma. Also wrapped in single quote.
// The minus sign is not used. You need to update the mp_excluded_categories with the slugs of your excluding categories.
$mp_excluded_categories = array('products');
// Page ID to redirect non-logged in users when viewing a single product page associated with an excluded category
$mp_redirect_archive_page_id = 123;
// Page ID to redirect non-logged in users when viewing a product category archive for an excluded category
$mp_redirect_single_page_id = 456;
// No Need to edit below here.
// ****************************************************************************
add_action( 'pre_get_posts', 'mp_product_page_exclude_proc', 99 );
add_filter( 'get_terms_args', 'mp_product_category_terms_proc', 99, 2 );
add_filter( 'posts_results', 'mp_product_get_posts_results', 99, 2);
/****************************************************************************
This function handles filtering of Products like on the main Store/Products list.
Should also handle display logic on the Product Category page. But not tested
*****************************************************************************/
function mp_product_page_exclude_proc( $wp_query ) {
global $mp_excluded_categories;
if (is_admin()) return;
if (!is_main_query()) return;
if ($wp_query->query_vars['post_type'] != 'product') return;
if (!get_current_user_id()) {
if ( ! empty( $wp_query->query_vars['tax_query'] ) && is_array( $wp_query->query_vars['tax_query'] ) ) {
$tax_query = $wp_query->query_vars['tax_query'];
} else {
$tax_query = array();
}
$tax_args = array(
'taxonomy' => 'product_category',
'field' => 'slug',
'terms' => $mp_excluded_categories,
'operator' => "NOT IN"
);
$tax_query[] = $tax_args;
$wp_query->set('tax_query', $tax_query);
}
}
/***************************************************************************
This function handles filtering on the get_terms WordPress function
So basically any place on the page showing a list of taxonomy terms
will be filters.
****************************************************************************/
function mp_product_category_terms_proc($args, $taxonomies) {
global $mp_excluded_categories;
// This prevents exclusion of sections in the admin area.
if (is_admin()) return $args;
// If the taxonomies function aregument is not passed. Bail.
if (!isset($taxonomies)) return $args;
// check to make sure we are working ONLY with out product_category taxonomy
if (!is_array($taxonomies))
$taxonomies = array($taxonomies);
if (array_search('product_category', $taxonomies) === false) return $args;
// Begin custon logic. If the user is not logged in...
if (!get_current_user_id()) {
if (!is_array($args['exclude'])) {
$args['exclude'] = explode(',', $args['exclude']);
}
$args['exclude'] = array_merge($args['exclude'], $mp_excluded_categories);
}
return $args;
}
/***************************************************************************
This function handles the filtering are the query has run. Here we check
the posts. If the user is viewing an product category archive we check if
it is for an exclude provide category. If the user is viewing a single
product then we check if that product is assigned to any excluded
product categories. If either case we redirect to a static page
****************************************************************************/
function mp_product_get_posts_results($posts, $this_wp_query) {
global $mp_excluded_categories, $mp_redirect_single_page_id, $mp_redirect_archive_page_id;
if (!get_current_user_id()) {
if ($this_wp_query->is_archive) {
if (isset($this_wp_query->query_vars['product_category'])) {
// The assumption here is that query_vars['product_category'] is always a single category not an array!
if (array_search($this_wp_query->query_vars['product_category'], $mp_excluded_categories) !== false) {
// If we found a match we want to redirect the user to a different page.
wp_redirect(get_permalink($mp_redirect_archive_page_id));
die();
}
}
} else if ($this_wp_query->is_single) {
if ((isset($this_wp_query->query_vars['post_type'])) && ($this_wp_query->query_vars['post_type'] == "product")) {
foreach($posts as $product_idx => $product) {
if (has_term( $mp_excluded_categories, 'product_category', $product )) {
// If we found a match we want to redirect the user to a different page.
wp_redirect(get_permalink($mp_redirect_single_page_id));
die();
}
}
}
}
}
return $posts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment