对于Woocommerce,我需要一个PHP代码片段来隐藏很少的产品ID的,我将为来宾和customers选择。
我的代码尝试:
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;
}
}但这不像我想的那样有效。
发布于 2020-07-18 00:19:36
如果您想完全隐藏该产品,请使用:
您可以使用产品的ID隐藏产品,并将其排除在产品查询之外:
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' );如果您想隐藏产品的add to cart按钮::
如果您希望列出带有价格的产品,但只隐藏“添加到购物车”按钮,则可以使用woocommerce_is_purchasable钩子进行此操作,该钩子将返回产品ids的false,因此将显示价格,但在“添加到购物车”按钮中会出现“无法购买产品”的通知。
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;
}发布于 2020-07-19 19:34:43
关于你的问题有两个不同的要求(一个在标题上,另一个在身体上有点不同)
1)。若要避免客人和客户购买某些定义的产品(隐藏或禁用customers按钮),请使用以下命令:
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;
}代码在您的活动子主题(或活动主题)的functions.php文件中。测试和工作。
2)。若要向客人和客户完全隐藏某些定义的产品,请使用以下内容:
// 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 );
}
}
}
}代码在您的活动子主题(或活动主题)的functions.php文件中。测试和工作。
相关:Hide a specific Woocommerce products from loop if they are in cart
https://stackoverflow.com/questions/62782639
复制相似问题