'Avoid add to cart for specific product categories if user is unlogged in Woocommerce
In Woocommerce, I am trying to disable specific product categories to be added to the cart for users that aren't logged in. i'm looking for a solution the last couple of days and after frustrated deleting the last code I found this but also doesn't do the job.
I'm using a certain plugin (TZ product tabs) to show products on other pages (so not only on the category and product page (this i know how to disable))
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
// replace a_category and another_category with the slugs of the categories you'd like to have the button removed from
if( is_product_category( array( 'gekoelde-bier', 'bierkoerier'))) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
// add_filter( 'woocommerce_is_purchasable', false );
}
}
Referenced from https://gist.github.com/rynaldos/560c621714b9680433cddf18e6a50305
My best guess is to check the category of the product when the "add to cart" button is pressed and based on that the product can be added to the cart or not.
Thanks in advance.
Solution 1:[1]
woocommerce_is_purchasable filter can do the job. It checks if a product is purchasable or not. If not purchasable, "add-to-cart" button is removed and can't be purchased.
So, here you will need to check if user is logged in or not, and product belongs to certain category, before disabling purchase ability.
This is how you can do it:
function wpso9800_remove_cart_button( $is_purchasable, $product ) {
//when logged in
if ( is_user_logged_in() ) {
return $is_purchasable;
}
//get categories
$categories = get_the_terms( $product->id, 'product_cat');
$my_terms_ids = array ( 1, 2 );//product category IDs
if ($categories && !is_wp_error($categories)) {
foreach ($categories as $cat) {
if ( in_array($cat->term_id, $my_terms_ids ) ) {
return false;
}
return $is_purchasable;
}
}
}
add_filter('woocommerce_is_purchasable','wpso9800_remove_cart_button', 10, 2);
**This is tested, and working.
Note: By default, $is_purchasable is set to true. You will need to return false, when you want to turn off purchasing.
Solution 2:[2]
I'm using option #2 because the being logged in for the listed parent categories is required. I was very careful to transfer exactly but for my site, it gives me the white screen of death. I placed this code directly in 'My Custom Functions' plugin listed under Settings>PHP Inserter. Can anyone see the problem?
add_filter('woocommerce_is_purchasable','conditional_purchasable_products', 20, 2);
function conditional_purchasable_products( $is_purchasable, $product) {
$terms= array( 'memberships', 'our-vendors', 'programs', 'uncategoriezed', 'unpublished-products');
$product_id= $product->get_id(); // The product ID
if( has_product_categories ( $categories, $product_id= 0) && ! is_user_logged_in() ){
$is_purchasable= false;
}
return $is_purchasable;
}
function has_product_categories ( $categories, $product_id = 0) {
$parent_term_ids = $categories_ids = array();
$product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
$categories = (array) $categories;
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy) ;
if( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}
foreach( get_the_terms( $product_id, $taxonomy) as $term){
if( $term->parent > 0){
$parent_term_ids[] = $term->parent;
$parent_term_ids[] = $term->term_id;
} else{
$parent_term_ids[] = $term->term_id;
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true: false;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ??? Sufi |
| Solution 2 | Penny |
