Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Created March 17, 2023 22:58
Show Gist options
  • Save Acephalia/c85c5c6d6646a401b5d66f342f99ec35 to your computer and use it in GitHub Desktop.
Save Acephalia/c85c5c6d6646a401b5d66f342f99ec35 to your computer and use it in GitHub Desktop.
Woocommerce Hide Products Until A Specific Product Is Purchased
// Hide Products Until A Specific Product Is Purchased
function ghostie_pepper_until_purchase( $purchasing_product_id ) {
// Check if user has purchased the purchasing product
if ( wc_customer_bought_product( get_current_user_id(), $purchasing_product_id ) ) {
return;
}
// If not, hide the specified products
$products_to_hide = array( 123, 456, 789 ); // replace with your product IDs
foreach ( $products_to_hide as $product_id ) {
$product = wc_get_product( $product_id );
if ( $product ) {
$product->set_catalog_visibility( 'hidden' );
$product->save();
}
}
}
add_action( 'wp', function() {
// Replace 789 with the ID of the product that needs to be purchased to unhide the other products
ghostie_pepper_until_purchase( 789 );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment