Stack Overflow Asked by Trooller on November 22, 2021
For Woocommerce, I need a PHP snippet that will hide few products ID’s I will select for guests and customers.
My code attempt:
function dma_restrict_product() {
$user = wp_get_current_user();
$user_meta = get_userdata($user->ID);
$user_roles = $user_meta->roles;
global $product;
if( in_array( 'customer', (array) $user_roles ) && ( is_single('3759') ) ) {
return true;
add_filter('woocommerce_is_purchasable', 'woocommerce_cloudways_purchasable');
function woocommerce_cloudways_purchasable($cloudways_purchasable, $product) {
return ($product->id == 3759 ? false : $cloudways_purchasable);
}
} else if( in_array('administrator', (array) $user_roles) ) {
return true;
} else {
return false;
}
}
But it doesn’t work as I would like.
There are 2 different request on your question (one in the title and another a bit different in the body):
1). To avoid guests and customers to purchase some defined products (hiding or disabling add to cart button), use the following:
add_filter( 'woocommerce_is_purchasable', 'restrict_purchases_on_defined_product', 10, 2 );
function restrict_purchases_on_defined_product( $is_purchasable, $product ) {
// For guest users or "customer" user role
if ( ! is_user_logged_in() || current_user_can( 'customer' ) ) {
// HERE define your product ID(s) that will noot be purchassable
$restricted_product_ids = array(37, 53, 70);
if ( array_intersect( array( $product->get_id(), $product->get_parent_id() ), $restricted_product_ids ) ) {
return false;
}
}
return $is_purchasable;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
2). To hide completely some defined products from guests and customers, use the following:
// Settings: HERE define your product ID(s) that will not be purchasable
function my_hidden_product_ids() {
return array(37, 53, 70);
}
// Hide some products from product loops
add_action( 'woocommerce_product_query', 'hide_defined_product' );
function hide_defined_product( $query ) {
// For guest users or "customer" user role
if ( ! is_user_logged_in() || current_user_can( 'customer' ) ) {
$hidden_product_ids = my_hidden_product_ids();
$query->set( 'post__not_in', $hidden_product_ids );
}
}
// Redirect some product pages to main shop page (for security)
add_action( 'template_redirect', 'redirect_defined_product_single_pages' );
function redirect_defined_product_single_pages( $query ) {
// For guest users or "customer" user role
if ( ! is_user_logged_in() || current_user_can( 'customer' ) ) {
$hidden_product_ids = my_hidden_product_ids();
if ( in_array( get_queried_object_id(), $hidden_product_ids ) ) {
wp_redirect( wc_get_page_permalink( 'shop' ) );
exit();
}
}
}
// Check and remove related cart items silently (for security)
add_action('woocommerce_before_calculate_totals', 'remove_specific_products_from_cart', 10, 1 );
function remove_specific_products_from_cart( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// For guest users or "customer" user role
if ( ! is_user_logged_in() || current_user_can( 'customer' ) ) {
$hidden_product_ids = my_hidden_product_ids();
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
if ( array_intersect( array( $cart_item['product_id'], $cart_item['variation_id'] ), $hidden_product_ids ) ) {
$cart->remove_cart_item( $cart_item_key );
}
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Related: Hide a specific Woocommerce products from loop if they are in cart
Answered by LoicTheAztec on November 22, 2021
If you want to hide the product completly:
You can hide products by using the ID of the product and exclude it from the product query:
function hide_my_product( $q ) {
$user = wp_get_current_user();
$user_meta = get_userdata($user->ID);
$user_roles = $user_meta->roles;
if ( !in_array( 'administrator', $user_roles ) ) {
// id of product to hide
$product_id = 3759;
// exclude the id from query
$q->set( 'post__not_in', $product_id );
}
}
add_action( 'woocommerce_product_query', 'hide_my_product' );
If you want to hide the add to cart button of product:
If you want to list the product with price, but only hide the "Add to Cart" button, you can do this with the woocommerce_is_purchasable
hook, that will return false for your product ids and therefore will show the price, but in the place of "Add to Cart" button the notice "Product cannot be purchased" appears.
add_filter( 'woocommerce_is_purchasable', 'hide_add_to_cart_button', 10, 2 );
function hide_add_to_cart_button( $purchasable = true, $product ) {
$user = wp_get_current_user();
$user_meta = get_userdata($user->ID);
$user_roles = $user_meta->roles;
if ( !in_array( 'administrator', $user_roles ) && $product->get_id() == 3759 ) {
$purchasable = false;
}
return $purchasable;
}
Answered by rank on November 22, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP