Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Hide products only show assigned products to certain user roles in WooCommerce

//Please give a thumbs up if this was helpfull

//So I can see 2 separate issues in this question.
//First one being "how to display only products belonging to certain user role"
//For this, you might wanna alter the woocommerce product query. You can do so by adding something like the following code to your theme's functions.php:

<?php
function filter_products_by_user_roles( $q ) {

    // alter the query only for logged users
    if (is_user_logged_in()) {
        // get user roles assigned to current profile
        $user_meta = get_userdata(get_current_user_id());
        $user_roles = $user_meta->roles;
        // create new tax_query variable with desired relation
        $tax_query = array('relation' => 'OR');
        // add a product_tag filter for each user role
        foreach ($user_roles as $role) {
            $tax_query[] = array(
                'taxonomy' => 'product_tag',
                'field' => 'slug',
                'terms' => $role, 
            );
        }
        // overwrite the default tax_query with our custom code
        $q->set( 'tax_query', $tax_query );
        // set additional attributes for the query
        $q->set( 'posts_per_page', '30' );
        $q->set( 'post_type', 'product' );
        $q->set( 'post_status', 'publish' );
    }

}
// hook the alteration to the original product query function
add_action( 'woocommerce_product_query', 'filter_products_by_user_roles' );
?>
//This code will take all of the user's roles and add them 1 by 1 to the product query filter based on product_tag, which is essential for this. The slug of assigned product tag must match the user role name. for example: user role is "VIP_user_2" so the tag assigned to desired products must be "VIP_user_2"
//Second one is "How to display different prices for different user roles"for this, we will have to edit woocommerce files, so first go through this guide on how to safely edit such files: https://woocommerce.com/document/template-structure/
//Once your theme is prepared, you can edit the woocommerce/loop/price.php with your own condition and the values / db fields you want to fill in for users
//The default code in price.php is:

<?php if ( $price_html = $product->get_price_html() ) : ?>
    <span class="price"><?php echo $price_html; ?></span>
<?php endif; ?>
Comment

PREVIOUS NEXT
Code Example
Php :: laravel gigapay resend invite to employee 
Php :: how to color php text 
Php :: specific function to Unflatten array 
Php :: hook de pré-commit se déclenche 
Php :: php google authenauthenticator 
Php :: auth guard (admin) is not defined laravel 8 
Php :: quitar elemento array php 
Php :: magento 2 get layout create block with cache 
Php :: instagram api error 
Php :: install PHP extension "amqp" not found, please install it 
Php :: update request php-salesforce-rest-api 
Php :: php undefined index 
Php :: gerar aquivo csv php 
Php :: Insert Data Into MySql Database Multiple Columns PHP Function 
Php :: in connection.php line 664: could not find driver (sql: select * from information_schema.tables where table_schema = news and table_name = migrations) in connector.php line 67: could not find driver 
Php :: session 
Php :: php get screen width 
Php :: read file and convert each line in array php 
Php :: how to pass value from one php page to another using session 
Php :: add password php file 
Php :: php csv to array with headers 
Php :: active class php 
Java :: Cannot resolve class android.support.design.widget.CoordinatorLayout 
Java :: java swing make window not resizable 
Java :: circular imageview android 
Java :: handler delay android 
Java :: copy array in java 2d 
Java :: java selenium new window 
Java :: how to install java 8 on aws linux 
Java :: how to print a matrix in java 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =