add_filter('woocommerce_is_purchasable', 'gt_purchasable_products', 10, 2); function gt_purchasable_products( $purchasable, $product ) { $purchasable = true; $is_a_member_blue = false; $is_a_member_red = false; $is_a_member_green = false; //Group name which makes all products non-purchasable $group_all_products = "Blue"; //Each group name below should be in the same order with its corresponding product name. //ie Red group is product10's group //List of groups by name $product_groups = array( 'Red', 'Green'); //List of products by name $product_names = array( 'product10', 'product 1'); $product_ids = array(); foreach ( $product_names as $product_name ) { if ( !is_null( $list_product = get_page_by_title( $product_name, OBJECT, 'product' ) ) ) { $product_ids[] = $list_product->ID; } } require_once( ABSPATH . 'wp-includes/pluggable.php' ); if ( $group = Groups_Group::read_by_name( $group_all_products ) ) { $is_a_member_blue = Groups_User_Group::read( get_current_user_id() , $group->group_id ); } foreach ( $product_groups as $group_name ) { if ( $group = Groups_Group::read_by_name( $group_name ) ) { $is_a_member[] = Groups_User_Group::read( get_current_user_id() , $group->group_id ); } } if ( $is_a_member_blue ) { remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 ); remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 ); $purchasable = false; } foreach ( $is_a_member as $is_true ) { if ( $is_true ) { if ( in_array( $product->id, $product_ids ) ) { $purchasable = false; } } } return $purchasable; }