I need to lock some woocommerce products by category to enable purchasing for "shareholders" role only. How can I do so?
Asked
Active
Viewed 867 times
0
-
1You can remove the Add to cart button from those category products and display a message that it can be bought by shareholders only – Logan Young Oct 21 '17 at 18:32
-
How? Can you please show me a specific code? – Evyatar Tal Oct 21 '17 at 20:03
-
What have you tried? There appear to be plenty of solutions via Google. Here's one: https://www.sellwithwp.com/how-do-i-restrict-products-to-members-only/ – helgatheviking Oct 22 '17 at 00:55
1 Answers
1
Here is the solution for your query that is hiding certain or specific category products add to cart button for all except shareholders roles users
You can enter your category ids in this function in array format
function get_resticted_category_ids() {
//Enter your ids here
return array( 9, 10, 11 );
}
Hiding Add To Cart button from shop page
add_filter('woocommerce_loop_add_to_cart_link','change_simple_shop_add_to_cart',10,2);
function change_simple_shop_add_to_cart( $html, $product ){
$product_cat_ids = $product->get_category_ids();
$restricted_ids = get_resticted_category_ids();
$common_ids = array_intersect( $product_cat_ids, $restricted_ids );
if( isset( $common_ids ) && $common_ids != null ) {
$user = wp_get_current_user();
$roles = $user->roles;
if( empty($roles) || !in_array('shareholders', $roles) ) {
$html = __('You must have a shareholder role to pruchase this product', 'text-domain');
}
}
return $html;
}
Hiding Add To Cart button from Simple Product Type Detail page
if ( ! function_exists( 'woocommerce_simple_add_to_cart' ) ) {
function woocommerce_simple_add_to_cart() {
if( check_for_specific_role() ) {
echo __('You must have a shareholder role to pruchase this product', 'text-domain');
}
else {
wc_get_template( 'single-product/add-to-cart/simple.php' );
}
}
}
Hiding Add To Cart button from Variable Product Type Detail page
if ( ! function_exists( 'woocommerce_variable_add_to_cart' ) ) {
function woocommerce_variable_add_to_cart() {
if( check_for_specific_role() ) {
echo __('You must have a shareholder role to pruchase this product', 'text-domain');
}
else {
global $product;
// Enqueue variation scripts
wp_enqueue_script( 'wc-add-to-cart-variation' );
// Get Available variations?
$get_variations = sizeof( $product->get_children() ) <= apply_filters( 'woocommerce_ajax_variation_threshold', 30, $product );
// Load the template
wc_get_template( 'single-product/add-to-cart/variable.php', array(
'available_variations' => $get_variations ? $product->get_available_variations() : false,
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_default_attributes(),
) );
}
}
}
Hiding Add To Cart button from Grouped Product Type Detail page
if ( ! function_exists( 'woocommerce_grouped_add_to_cart' ) ) {
function woocommerce_grouped_add_to_cart() {
if( check_for_specific_role() ) {
echo __('You must have a shareholder role to pruchase this product', 'text-domain');
}
else {
global $product;
$products = array_filter( array_map( 'wc_get_product', $product->get_children() ), 'wc_products_array_filter_visible_grouped' );
if ( $products ) {
wc_get_template( 'single-product/add-to-cart/grouped.php', array(
'grouped_product' => $product,
'grouped_products' => $products,
'quantites_required' => false,
) );
}
}
}
}
Hiding Add To Cart button from External Product Type Detail page
if ( ! function_exists( 'woocommerce_external_add_to_cart' ) ) {
function woocommerce_external_add_to_cart() {
if( check_for_specific_role() ) {
echo __('You must have a shareholder role to pruchase this product', 'text-domain');
}
else {
global $product;
if ( ! $product->add_to_cart_url() ) {
return;
}
wc_get_template( 'single-product/add-to-cart/external.php', array(
'product_url' => $product->add_to_cart_url(),
'button_text' => $product->single_add_to_cart_text(),
) );
}
}
}
Function that checks and return true only for shareholders roles users and false for guest users and other roles users
function check_for_specific_role() {
global $product;
$product_cat_ids = $product->get_category_ids();
$restricted_ids = get_resticted_category_ids();
$common_ids = array_intersect( $product_cat_ids, $restricted_ids );
if( isset( $common_ids ) && $common_ids != null ) {
$user = wp_get_current_user();
$roles = $user->roles;
if( empty($roles) || !in_array('shareholders', $roles) ) {
return true;
}
}
return false;
}

Logan Young
- 385
- 2
- 9