WooCommerce: Conditionally Display Content to Customers [Shortcode]

Today’s customization will feature a custom shortcode that you can use to display content only to logged in customers: [customers-only]content for customers here e.g. video iframe[/customers-only]

This is perfect for membership sites, LMS platforms, paywalled content.

In my case, only premium Club members with a ‘CLASS’ pass can watch upcoming WooCommerce masterclasses and past recordings, so I needed the shortcode to hide the video to logged out users or logged in customers without a pass.

By wrapping the video iframe HTML inside the shortcode, I can show the video to logged in customers who purchased a given product ID, or alternatively show an error message, which you can see in action here, in the “The recording is now available!” section: https://www.businessbloomer.com/class/behind-the-scenes-how-i-run-business-bloomer/

So, how did I do it? Find out below, and hope you can use it too!

This is the error message that appears to logged out OR logged in customers who haven’t purchased a specific WooCommerce product. If the logged in user, on the other hand, bought the pass, they will see the hidden content instead – a video iframe in this case.

PHP Snippet: Shortcode to Display Conditional Content to Logged In Customers Who Purchased a Specific Product

/**
 * @snippet       Customer Conditional Content Shortcode
 * @usage         [customers-only]content[/customers-only]
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_shortcode( 'customers-only', 'bbloomer_conditional_content_club_members_only' );

function bbloomer_conditional_content_club_members_only( $atts, $content = "" ) {

   // RETURN ERROR MESSAGE IF LOGGED OUT OR DID NOT PURCHASE PROD ID 12345
	if ( ! is_user_logged_in() || ! wc_customer_bought_product( '', get_current_user_id(), 12345 ) ) return '<div class="woocommerce-error"> Sorry, this video is only visible to <b>logged in Business Bloomer Club members with a "<i>CLASSES</i>" or "<i>SPONSOR</i>" pass</b>.<br>If you joined already, please <a href="' . esc_url( wc_get_page_permalink( 'myaccount' ) ) . '">log in</a>.<br>Otherwise, here are 5 reasons you should <a href="/club/">join the Club</a>.</div>';

   // OTHERWISE RETURN CONTENT
	return $content;

}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Display All Products Purchased by User
    When a WooCommerce customer is logged in, you might want to show them the list of previously purchased products (maybe in a custom “My Account” tab). This is helpful when customers tend to buy the same products over and over again, and therefore you can help them “order again” without having them to search the […]
  • WooCommerce Conditional Logic – Tags, Examples & PHP
    The WooCommerce and WordPress conditional tags (“WooCommerce and WordPress Conditional Logic”) can be used in your functions.php to display content based on certain conditions. For example, you could display different content for different categories within a single PHP function.
  • WooCommerce: Display Out of Stock Products (Shortcode)
    A client of mine wanted to show out of stock products on a separate page – so I coded a simple shortcode for you all! You can use this shortcode for different goals. For example, you might want to display what products you’ve sold to enhance customer trust / social proof. So let’s see (1) […]
  • WooCommerce: Recently Viewed Products (Shortcode)
    Currently, you can use a widget or a block to have the user see the list of products they recently viewed. But for now, let’s create our own shortcode… and let’s take advantage of the existing Business Bloomer ClubRated 5.00 out of 5 $9.00 – $599.00 /once Whether you’re only starting with WooCommerce or you’re […]
  • WooCommerce: Exclude Category from ‘product_categories’ Shortcode
    Sometimes solutions are very simple, and you don’t need rocket science to fix your issue! A client of mine needed to hide a category from the Product Categories Shortcode ( BloomerArmada (1) WooCommerce Mini-Plugins (30) ); in fact, there is no parameter that allows you to “exclude” a given product category such as “uncategorized” or […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *