Conditionally displaying info to Wholesale Customers

If you are using my WooCommerce Wholesale Ordering extension, and you have some info that you only want to show to Wholesale Customers, my plugin provides a couple of publicly accessible conditional PHP functions that you can use in your theme templates or functions.php file.

There are two functions that will return true or false depending on the current user viewing the site and on the wholesale ordering settings:

woocom_wholesale_is_wholesale_customer()

The above function will return true if the current user has the Wholesale Customer role, or if the current user is admin and you have my plugin set up to treat admin as a wholesale customer.

woocom_wholesale_show_wholesale_prices()

The above function adds an additional check to see if you have wholesale ordering store enabled (in the general settings of my plugin). It calls the first function to check if the current user is wholesale, and then if you also have the main setting in my plugin enable, it will return true.

Most of the time, I would recommend using the second function.

To use these in a template file, you just need to use a PHP “if” statement to check one of the functions, wrapping that “if” statement around the content that you only want wholesale customers to see.

Let’s take the example of showing the custom isbn postmeta field that I used in the example for my post on Adding and displaying custom WooCommerce Product fields.  If we only want to show the ISBN field to wholesale customers, we simply wrap that line I added to the meta.php template file within a PHP conditional “if” statement, like this:

<?php if (woocom_wholesale_show_wholesale_prices()): ?>
   <span class="sku_wrapper isbn_wrapper">ISBN: <span class="isbn"><?php echo esc_html( get_post_meta(get_the_ID(), 'isbn', true) ); ?><span></span>
<?php endif; ?>

Note the formatting, there is the first line which starts the conditional ‘if’ statement, and then you need an ‘endif’ PHP statement to end the conditional statement.  You can put as much content as you want between those two lines, on as many lines as you need in your template.

To use the same conditional functions in your theme’s functions.php file (which is mostly PHP as opposed to the mostly HTML in template files), you would need to create a function that is hooked to some action or filter hook to create the output where you want it.  So, for example, if you wanted to output the ISBN info from the same example, but wanted to use a WooCommerce hook to place it within a template, instead of creating your own template, we could do something like this:

If you look at the meta.php template file in WooCommerce you’ll see there is a “do_action” call before and after the display of the product meta.  So, let’s say we want to show our ISBN info before the rest of the product meta that WooCommerce shows in that template file. For that, we want to tap into the action hook ‘woocommerce_product_meta_start’.  So, you could add this code snippet to your theme’s functions.php file to accomplish the same thing as our above example without modifying the WooCommerce meta.php template (although this time the ISBN info will appear above the SKU and all other product meta):

add_action('woocommerce_product_meta_start', 'add_custom_field_info');
function add_custom_field_info() {
   global $post;
   if (woocom_wholesale_show_wholesale_prices()){
      echo '<span class="sku_wrapper isbn_wrapper">ISBN: <span class="isbn">' . esc_html( get_post_meta($post->ID, 'isbn', true) ) . '<span></span>';
   }
}

Similarly you can use any WordPress or WooCommerce action or filter hooks to place custom content for Wholesale Customers in many different places throughout your site.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.