在 WooCommerce 中对未经授权的用户完全隐藏产品

如果用户不是特定角色(例如验证买家),我正在尝试从未登录的用户中完全删除一个/多个产品。


我已经能够使用下面的代码创建一个名为 Verified Buyer 的新角色;


add_role(

    'verified_buyer',

    __( 'Verified Buyer', 'text-domain' ),

    array(

       'read'         => true,  

        'edit_posts'   => false,

    )

);

//This Role is same role capability as the WooCommerce Customer role

我还使用以下代码向 WooCommerce 添加新产品页面添加了一个复选框


function hide_product_from_unathorised_users() {

  $args = array(

    'id' => '_hide_from_unauthorize_users',

    'label' => 'Hide Product from unauthorized users',

    'desc_tip' => true,

    'description' => __('Check this to hide this product from unauthorized users', 'text-domain')

  );

  woocommerce_wp_checkbox( $args );

}


add_action( 'woocommerce_product_options_advanced', 'hide_product_from_unathorised_users' );


// Save Fields

function product_custom_fields_save($post_id){

    // Custom Product Text Field

    $hide_product_unathorised_users = isset( $_POST['_hide_from_unauthorize_users'] ) ? 'yes' : 'no';

        update_post_meta($post_id, '_hide_from_unauthorize_users', esc_attr( $hide_product_unathorised_users ));

}

add_action('woocommerce_process_product_meta', 'product_custom_fields_save');

现在我有这两个选项(用户角色和一个复选框来知道要隐藏哪个产品)......如果满足以下条件,我想隐藏此类产品;


完全隐藏产品(甚至从搜索查询中)如果;

1. 产品上的复选框被选中且用户未登录

2. 产品上的复选框被选中且用户已登录且未验证买家或管理员角色

像这样


function hide_product_completely_conditionally() {


global $post;


$hide_product_checkbox = get_post_meta( $post->ID, '_hide_from_unauthorize_users', true )


$user = wp_get_current_user();

$authorized_user_role = in_array( 'verified_buyer', (array) $user->roles );

$admin_role = in_array( 'administrator', (array) $user->roles );


    if ( ($hide_product_checkbox == 'yes' && !is_user_loggedin()) || ($hide_product_checkbox == 'yes' && is_user_loggedin() && (!$authorized_user_role || !$admin_role) ) ) {


     //(...) HIDE SUCH PRODUCT COMPLETELY CODE THAT I'M NOT SURE HOW TO WRITE


    }

}

提前感谢您的帮助。


HUWWW
浏览 167回答 2
2回答

不负相思意

当用户不被允许时,以下代码将根据您的自定义产品字段过滤产品(如果他们尝试手动访问受保护的产品,则会将他们重定向到商店页面)。// Conditional function checking for authorized usersfunction is_authorized_user(){    if ( is_user_logged_in() ) {        $user = wp_get_current_user();        $caps = $user->allcaps;        if ( ( isset($caps['edit_product']) && $caps['edit_product'] )        || in_array( 'verified_buyer', $user->roles ) ) return true;    }    else return false;}// Filter product query (and search) from unauthorized usersadd_filter( 'woocommerce_product_query_meta_query', 'only_authorized_users_meta_query', 10, 2 );function only_authorized_users_meta_query( $meta_query, $query ) {    // Hide specific products from unauthorized users    if( ! is_authorized_user() && ! is_admin() ) {        $meta_query['relation'] = 'OR';        $meta_query[] = array(            'key'     => '_hide_from_unauthorize_users',            'value'   => 'no',            'compare' => '='        );        $meta_query[] = array(            'key'     => '_hide_from_unauthorize_users',            'compare' => 'NOT EXISTS'        );    }    return $meta_query;}// Security: Redirect unauthorized users if accessing prodtected productsadd_action( 'template_redirect', 'only_authorized_users_redirect' );function only_authorized_users_redirect() {    // Specific products redirect for unauthorized users (to be sure)    if( is_product() && ! is_authorized_user()    && get_post_meta( get_the_id(), '_hide_from_unauthorize_users', true ) === 'yes' ) {        wp_safe_redirect( get_permalink( wc_get_page_id( 'shop' ) ) );        exit;    }}代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。您可以不使用其他用户角色,而是:1)使用WC_Customer is_paying_customer布尔属性,如: if( WC()->customer->get_is_paying_customer() ) {     // Is a playing customer } else {     // NOT a playing customer }2)或添加自定义用户元为: update_user_meta( get_current_user_id(), 'allowed_customer', '1' );然后您将使用以下方法进行检查: if( get_user_meta( get_current_user_id(), 'allowed_customer', true ) ) {     // Allowed customer } else {     // NOT Allowed customer }

吃鸡游戏

当您说“删除”时,我假设您实际上是在尝试隐藏该产品。所以,使用pre_get_posts动作钩子是你的方法。下面的代码会隐藏有其领域的任何产品_hide_from_unauthorize_users设置为yes从没有被登录的用户中,并从已登录的用户,但不是一个verified_buyer,也不是administrator。将下面的代码片段放在您的functions.php文件中并注意注释:<?php/**&nbsp;* @param WP_Query $query&nbsp;*/function _hide_products_from_certain_users( $query ) {&nbsp; &nbsp; if ( is_admin() ) {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Create the query which will make sure only products that are allowed to bee seen will show up.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; $meta_query[] = array(&nbsp; &nbsp; &nbsp; &nbsp; 'key'&nbsp; &nbsp; &nbsp;=> '_hide_from_unauthorize_users',&nbsp; &nbsp; &nbsp; &nbsp; 'value'&nbsp; &nbsp;=> 'yes',&nbsp; &nbsp; &nbsp; &nbsp; 'compare' => '!=',&nbsp; &nbsp; );&nbsp; &nbsp; $user = wp_get_current_user();&nbsp; &nbsp; // If user is not logged in.&nbsp; &nbsp; if ( ! is_user_logged_in() ) {&nbsp; &nbsp; &nbsp; &nbsp; $query->set( 'meta_query', $meta_query );&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $authorized_user_role = in_array( 'verified_buyer', (array) $user->roles );&nbsp; &nbsp; &nbsp; &nbsp; $admin_role&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= in_array( 'administrator', (array) $user->roles );&nbsp; &nbsp; &nbsp; &nbsp; // If the current user is not a verified_buyer nor an administrator.&nbsp; &nbsp; &nbsp; &nbsp; if ( ! $authorized_user_role && ! $admin_role ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->set( 'meta_query', $meta_query );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}add_action( 'pre_get_posts', '_hide_products_from_certain_users' );顺便说一句,你的代码中有一些语法错误,我修复了它们。
打开App,查看更多内容
随时随地看视频慕课网APP