Skip to content

Instantly share code, notes, and snippets.

@pelmered
Last active December 22, 2017 18:11
Show Gist options
  • Save pelmered/a363489ea5c94c3fe77c to your computer and use it in GitHub Desktop.
Save pelmered/a363489ea5c94c3fe77c to your computer and use it in GitHub Desktop.
Make WooCommerce products purchasable by category
add_action('wp', 'prefix_theme_setup', 99);
function prefix_theme_setup()
{
add_filter( 'woocommerce_is_purchasable', 'prefix_is_purchasable', 20, 2);
}
function prefix_is_purchasable($purchasable, $product)
{
$user = wp_get_current_user();
$user_meta = get_user_meta( $user->id );
if(str_replace(' ', '', $user_meta['billing_postcode']) == '12312')
{
return true;
}
return false;
/*
$not_purchasable_cat_ids = array(5, 8, 9, 11);
$categories = get_the_terms($product->ID, 'product_cat');
foreach($categories AS $category)
{
if( in_array( $category->term_id, $not_purchasable_cat_ids ) )
{
return false;
}
return true;
}
/*
//Get ID of root category. If the product is in this caregory or any ancestors to this category it will be purchasable
$store_root_cat_id = 5;
//$store_root_cat_id = get_store_root_category_id();
$categories = wp_get_post_terms($product->ID, 'product_cat');
foreach($categories AS $category)
{
if(term_is_ancestor_of($store_root_cat_id, $category, 'product_cat'))
{
return true;
}
}
return false;
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment