最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

WooCommerce - Show different terms and conditions for different user roles

programmeradmin2浏览0评论

I'm setting up a WooCommerce shop and it will have two types of customer/user roles - a standard retail customer/user role and a wholesale customer/user role.

Here's the snag that I've hit - when a Wholesale customer is logged in, I need the Terms and Conditions link on the checkout page to go to a different T&C's page.

I've tried a couple of different functions in functions.php to replace the link text but they don't work.

Does anyone out there have any suggestions that I can try??

Thanks,

Bev

I'm setting up a WooCommerce shop and it will have two types of customer/user roles - a standard retail customer/user role and a wholesale customer/user role.

Here's the snag that I've hit - when a Wholesale customer is logged in, I need the Terms and Conditions link on the checkout page to go to a different T&C's page.

I've tried a couple of different functions in functions.php to replace the link text but they don't work.

Does anyone out there have any suggestions that I can try??

Thanks,

Bev

Share Improve this question asked Jun 24, 2019 at 16:37 ThinkLoveDigitalThinkLoveDigital 11 bronze badge 3
  • Did you create the new role with add_role()? How are you displaying the T&C link? – RiddleMeThis Commented Jun 24, 2019 at 17:22
  • @RiddleMeThis The Wholesale role is created as part of a plugin that's been installed (WooCommerce Wholesale Pro Suite). Currently, the T&C link is being displayed using the standard WooCommerce functionality. But what I've done is amend the terms.php file, within the checkout folder of the WooCommerce templates and I've found a function that will check if a user has a particular role - so I'm just doing a simple if statement to display a different T&C's link if the user has the wholesale role. Not sure if it's the best way but is working for me. – ThinkLoveDigital Commented Jun 26, 2019 at 11:06
  • You should post your solution as an answer and accept it. – RiddleMeThis Commented Jun 26, 2019 at 13:22
Add a comment  | 

1 Answer 1

Reset to default 0

The Wholesale role was created as part of a plugin that's been installed (WooCommerce Wholesale Pro Suite). Currently, the T&C link is being displayed using the standard WooCommerce functionality.

But what I've done is amend the terms.php file, within the checkout folder of the WooCommerce templates (copying it to my theme folder) and I've found a function that will check if a user has a particular role - so I'm just doing a simple if statement to display a different T&C's link if the user has the wholesale role.

This is the code I've used:

// flag to indicate if the user is not assigned the wholesale user role
$wholesale_user_role = 0;
// get the roles of the currently logged in user
if ($user_id) $user = get_userdata($user_id);
else $user = wp_get_current_user();
    if (empty($user)) return false;
        // loop through each of the roles
        foreach ($user->roles as $role) {
            // check to see if the user has been assigned the wholesale user role
                if (in_array($role, array('ignite_level_5aedba974920a'))) {
                // current logged in user is assigned the wholesale user role, so set the flag to 1 (true)
                    $wholesale_user_role = 1;
                }
            }
            // check to see if the currently logged in user has the wholesale user role 
            if(!$wholesale_user_role) {
                // isn't assigned the wholesale user role, so display the standard t&c's text and link
            ?>
                <span class="woocommerce-terms-and-conditions-checkbox-text"><?php wc_terms_and_conditions_checkbox_text(); ?></span>&nbsp;<span class="required">*</span>
            <?php
            } else { 
            // current user is assigned the wholesale role, so display wholesale T&C's ?>
                <span class="woocommerce-terms-and-conditions-checkbox-text">I have read and agree to the website <a href="https://www.example/wholesale/wholesale-terms-conditions/" class="woocommerce-terms-and-conditions-link" target="_blank">terms and conditions</a></span>&nbsp;<span class="required">*</span>
            <?php
            }
            ?>

Not sure if it's the best way but is working for me.

发布评论

评论列表(0)

  1. 暂无评论