Reputation: 13
Hi can anyone lead me in the right direction. This is what I am trying to achieve with my WooCommerce store. I have a store that has categories available to the general public but one category is a specialized category and requires approval to purchase so I want to create a specific user role for this category.
Ive been able to achieve hidden prices on one category but cant seem to work out how to restrict the purchase of that category and reveal prices to a specific user role.
So for example
The parent category Sample category and all its sub categories is to have a hidden price unless a user role “sample category Customer” is logged in. Instead of a price it will have the text “register for price”
Only the user role sample category Customer can purchase items from the sample category and its sub categories. Any other user role will be prohibited from purchasing items from the category but the items will still be visible to them with a hidden price.
The sample category user can purchase from anywhere
All other categories to have visible prices for all user roles. All categories including the Sample category (which will have hidden price )to be visible for everyone
thank you for any help
Vanessa
Upvotes: 1
Views: 1459
Reputation: 1027
Below, I have modified some code I've written previously for wholesale customers. The code removes the price and 'Add to Cart' button, and prevents those products being purchased at all.
add_action( 'template_redirect', 'hide_price_and_add_cart' );
// The first hook that is safe to get post id is template_redirect
function hide_price_and_add_cart() {
$postID = get_queried_object_id();
if ( ( is_product( $postID ) && has_term( 'sample_category', 'product_cat' ) ) ||
// if single product pages in the category "sample_category"
( is_product_category( 'sample_category' ) )
// or, if product category pages "sample_category"
) {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
if ( ! in_array( 'sample_category_customer', (array) $user->roles ) ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
// remove 'Add to Cart' from product category page
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// remove 'Add to Cart' from single product page
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
// remove price from product category page
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
// remove price from single product page
add_action( 'woocommerce_single_product_summary', 'print_login_to_access', 31 );
// Print 'Register for price' on single product page
add_action( 'woocommerce_after_shop_loop_item', 'print_login_to_access', 11 );
// Print 'Register for price' on product category page
add_filter( 'woocommerce_is_purchasable', '__return_false');
// Finally, let's just make sure the product cannot be purchased...
}
} else {
// If user is not logged in, hide everything as well
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 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
add_action( 'woocommerce_single_product_summary', 'print_login_to_access', 31 );
add_action( 'woocommerce_after_shop_loop_item', 'print_login_to_access', 11 );
add_filter( 'woocommerce_is_purchasable', '__return_false');
}
}
}
function print_login_to_access() {
echo '<p>Register for price</p>';
}
Note - this code only works for products listed in the parent category "Sample Category". It will work for products in sub categories, if they are also listed in the parent cat. If you don't want them to be listed in the parent cat as well - only the sub cats - you'll need some code to recursively drill down and identify the parent.
Upvotes: 1