我需要按类别锁定一些 woocommerce 产品,以便仅允许“股东”角色购买。我怎样才能做到这一点?
这是针对您的查询的解决方案,它为除股东角色用户之外的所有用户隐藏某些或特定类别产品“添加到购物车”按钮
您可以在此函数中以数组格式输入您的类别 ID
function get_resticted_category_ids() {
//Enter your ids here
return array( 9, 10, 11 );
}
从商店页面隐藏“添加到购物车”按钮
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;
}
从简单产品类型详细信息页面隐藏“添加到购物车”按钮
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' );
}
}
}
从可变产品类型详细信息页面隐藏“添加到购物车”按钮
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(),
) );
}
}
}
从分组产品类型详细信息页面隐藏“添加到购物车”按钮
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,
) );
}
}
}
}
从外部产品类型详细信息页面隐藏“添加到购物车”按钮
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(),
) );
}
}
}
仅对股东角色用户检查并返回 true ,对来宾用户和其他角色用户检查并返回 false 的函数
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;
}