【发布时间】:2015-06-09 22:27:50
【问题描述】:
寻找可以帮助我根据角色限制 woocommerce 产品或产品类别的插件。
假设我只想向批发买家销售散装产品。
任何帮助都很棒,谢谢!
【问题讨论】:
标签: wordpress woocommerce
寻找可以帮助我根据角色限制 woocommerce 产品或产品类别的插件。
假设我只想向批发买家销售散装产品。
任何帮助都很棒,谢谢!
【问题讨论】:
标签: wordpress woocommerce
这是我设法根据角色隐藏产品的方法:
首先,我在产品选项库存部分添加了一个复选框,以使管理员能够根据他们的选择隐藏产品:
add_action( 'woocommerce_product_options_stock_status', 'hide_if_available_to_user_role' );
function hide_if_available_to_user_role(){
woocommerce_wp_checkbox( array( 'id' => '_hide_from_users', 'wrapper_class' => 'show_if_simple show_if_variable', 'label' => __( 'Hide this product from specific roles?', 'customhideplugin' ) ) );
}
然后,当帖子更新时,我将此选择保存在实际帖子中。
add_action( 'woocommerce_process_product_meta', 'hide_save_product_meta' );
function hide_save_product_meta( $post_id ){
if( isset( $_POST['_hide_from_users'] ) ) {
update_post_meta( $post_id, '_hide_from_users', 'yes' );
} else {
delete_post_meta( $post_id, '_hide_from_users' );
}
}
这就是我获得当前用户角色的方式。
function getCurrentUserRole( $user = null ) {
$user = $user ? new WP_User( $user ) : wp_get_current_user();
return $user->roles ? $user->roles[0] : false;
}
现在查询产品。如果当前用户角色与以下角色匹配,则照常显示产品。 否则,根据上面的代码设置查询...
add_action( 'woocommerce_product_query', 'hide_product_query' );
function hide_product_query( $q ){
if((getCurrentUserRole() == 'editor' ) || (getCurrentUserRole() == 'administrator' )){
return false;
} else {
$meta_query = $q->get( 'meta_query' );
if ( get_option( 'woocommerce_hide_out_of_stock_items' ) == 'no' ) {
$meta_query[] = array(
'key' => '_hide_from_users',
'compare' => 'NOT EXISTS'
);
}
$q->set( 'meta_query', $meta_query );
}
}
【讨论】:
要实现这一点,您可以使用 Free Groups 插件。但是为此,您必须将所有批发商添加到一个组中,例如批发组 1。然后在编辑您可以访问的任何产品时,将批发商组 1 添加到那里。该产品现在只能由批发商组 1 中的用户看到。
【讨论】:
我尝试了几个不同的插件来尝试实现这一点。我最终选择了这个,因为它易于理解,并且可以根据产品、标签、类别和自定义分类法显示/隐藏。 WooCommerce Products Visibility
【讨论】: