19th Jan '23
/
1 comment

Check if the User Purchased Any of the Specified Products

This Pro tutorial provides the steps for setting up a dynamic data condition on any element in Bricks so it can be output only if the logged-in user has purchased any of the WooCommerce products specified by their ID(s).

After adding the code for a custom function named bl_has_user_purchased_any_of_these_products, you will be able to add a condition like this:

{echo:bl_has_user_purchased_any_of_these_products(2076, 2164)}

This Section will then be rendered only if the currently logged-in user bought at least one of the two specified products (having the ID of 2076 or 2164). For everyone else incl. the guest visitors, the Section will not be output.

To conditionally check and output if the user has purchased a single product with the ID of 2076:

{echo:bl_has_user_purchased_any_of_these_products(2076)}

Step 1

Add the following in child theme's functions.php (w/o the opening PHP tag) or a code snippets plugin:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 609 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Conditional Output based on Manual Excerpt in Bricks

Conditional Output based on Manual Excerpt in Bricks

How to display a post’s excerpt only if it has been manually set.
Categories:
Pro
How to Modify Products Element Query in Bricks to Output Child Products

How to Modify Products Element Query in Bricks to Output Child Products

This Pro tutorial provides the steps to limit WooCommerce products output by Bricks' Products element on single product pages to only the children of the…
Conditionally Outputting Based on Query Count in Bricks

Conditionally Outputting Based on Query Count in Bricks

Update on 22 Sep 2023: There's a much simpler built-in method now. See this post. In the Bricks Facebook group a user asks: Is there…
Categories:
Pro
Media File Attachment Data in Bricks

Media File Attachment Data in Bricks

With a 'Media' query loop, this tutorial shows how we can retrieve various attachment-specific properties inside of the loop.
Categories:
Pro
ACF Repeater sub field value dynamic data condition in Bricks

ACF Repeater sub field value dynamic data condition in Bricks

This Pro tutorial provides the steps to output an element on single posts (can be of any post type) only if a specified ACF (Pro)…
Pro
Day of Week Condition in Bricks

Day of Week Condition in Bricks

Displaying elements conditionally based on the current day of the week.
Categories:
Pro
Checking if the current Page/Post has Children i.e. is a Parent in Bricks

Checking if the current Page/Post has Children i.e. is a Parent in Bricks

Shows how we can check whether the current Page or Post (of any hierarchical post type) is a parent
Categories:
Month-based Background Image in Bricks

Month-based Background Image in Bricks

How to have different background images for an element for different quarters in a year.
Categories: