我需要按类别锁定一些woocommerce产品,以便仅为“股东”角色购买。我怎么能这样做?
答案 0 :(得分:1)
以下是您的查询解决方案隐藏某些或特定类别产品添加到购物车按钮除了股东角色用户
您可以在此功能中以数组格式输入类别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;
}