Search code examples
phpwordpresswoocommerceroles

Woocommerce Role based Product Pictures


I'm developing a woocommerce website and would appreciate some help regarding an issue I am facing.

I have already set up 2 new user roles (Silver, Gold) and adjust the prices accordingly. So far so good.

What I want now is the ability of showing some of the product images only for those 2 new roles I've added. Not to the default customers.

Is this somehow possible? I've searched for a plugin that could do the job but couldn't find one. I also tried building the below custom function but could not find out how to distinct showing/hidden images.

function img_by_user_role {
$user = wp_get_current_user();
$allowed_roles = array('silver', 'gold');
<?php if( array_intersect($allowed_roles, $user->roles ) ) {  ?> 
   //stuff here for allowed roles
<?php } ?>

Any help would be very much appreciated, thank you for your time..


Solution

  • As 'mrhossen' suggested I was able to solve the problem using ACF plugin.

    For anyone who may face the same problem, this is how I managed to do so.

    1. Created a field Group named "Custom_Gallery"
    2. Populate the fields as shown in the screenshot
    3. Selected "product" as location
    4. On the product page I uploaded my custom field image
    5. I used the below snippet in my child-theme single-product.php template where I wanted the Image to show

    Screenshot - ACF Settings

    <?php if( current_user_can('silver') || current_user_can('gold') ) {  
    
    $image = get_field('image');
    
    if( !empty($image) ): ?>
    
    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    
    <?php endif; } ?>   
    

    Thanks again for your help, much appreciated!