Show element based on Product Purchase

Hey folks,

I have been trying to rack my brain and Google … but I cannot find how one can use “conditions” to hide a element on a page based on a product they purchased with Woocommerce.

Any ideas?

Cheers

Hey @shingen,

you could use the bricks/element/render hook and some custom PHP. Should look something like this:

add_filter( 'bricks/element/render', function( $render, $element ) {
  if ( $element->id !== 'YOUR_ELEMENT_ID_HERE' ) {
    return $render;
  }
  
  if ( ! is_user_logged_in() ) {
    return $render;
  }
  
  global $product;
  
  if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
     // User bought product -> do not render the element
    return false;
  } else {
     // User did not buy the product yet -> render the element
    return true;
  }
}, 10, 2 );

Note: Depending on your exact setup and use case this code might need some adjustments.

Best,

André

3 Likes

Hey André,

That is perfect, thank you for very much! =)

Cheers