Hi, I’m trying to create a section of my Woocommerce site that is only accessible to my Patreon backers. Any suggestions on how to do that?
I tried using the plugin, but that seems primarily focused on locking posts. I tried creating an item in my shop that could only be purchased by Patreon supporters, but the only thing the plugin hid was the product description (image here). Anyone could see the thumbnail (which should have been locked) and order the yo-yo without signing in to Patreon.
Ideally, I’d have a section of my ecommerce shop that only Patreon supporters could see and order from.
And yes, with the current setup, the plugin would only hide product description - since it treats it as a post.
To prevent viewing and ordering of patron-only products, it would be necessary to hook into woocommerce ordering process and do a check for pledge level of the user.
Patreon_Wordpress::getUserPatronage();
…would give you the pledge level of the user. Checking if that meets the minimum amount you may set for this, (ie, 5, etc) then you can prevent or allow checkout after that.
woocommerce_check_cart_items action hook may be useful.
There is an implementation for preventing checkout if cart contains certain products:
You can duplicate this function, insert the pledge check somewhere, and hijack the category-checking code of the function and instead do pledge checking.
Also, the visual hooks for checkout page template are below - these can help you to display messages to users if they are not patrons:
But my main hope is to block that category of my shop from even being visible to non-backers. I’m worried that if non-patrons can see items (and place them in their cart along with items from the rest of my shop), they may just rage quit and abandon their cart.
I also like that hiding the items adds a little mystery.
In the category listing page (or any search pages), just check for pledge level of the user with earlier mentioned function. If its lower than what you would desire, then just abort the page with a message or any other way. Best would be to just avoid printing any result but render the page.
Note that you will still need to implement the earlier suggestion too, because even if the user may not see the product from category page, s/he may find it through other means (google etc) and just check out.
Hi docpop! I’ve had this on the backburner for awhile, but wrote this function today based on codebard’s suggestions.
This is a bit outside of my wheelhouse, so take it all with a grain of salt, but it’s working on my site for now. Just put any product you want to reserve for patrons in the category “Members Only,” and change the url to your Patreon account, and the function should work as is.
// Limit Members Only WooCommerce Category to Patreon Patrons
function restrict_members_only_products_to_patrons() {
// set the slug of the category for which we disallow checkout
$category = 'members-only';
// get the product category
$product_cat = get_term_by( 'slug', $category, 'product_cat' );
// sanity check to prevent fatals if the term doesn't exist
if ( is_wp_error( $product_cat ) ) {
return;
}
// check if this category is the only thing in the cart
if ( check_if_user_is_not_patron() ) {
if ( is_category_in_cart( $category ) ) {
// render a notice to explain why checkout is blocked
wc_add_notice( sprintf( 'Hi there! I looks like your cart contains something that is only for members. You can sign up to be a member right now on <a href="https://patreon.com/killermann" title="Become a member on Patreon">Patreon</a> and we will be all set, otherwise you will need to remove that item from your cart to check out.'), 'error' );
}
}
}
function check_if_user_is_not_patron() {
$user_patronage = Patreon_Wordpress::getUserPatronage();
if ( $user_patronage >= 1 ) {
return false;
}
return true;
}
function is_category_in_cart($category) {
// check each cart item for our category
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// if a product is not in our category, bail out since we know the category is not alone
if ( has_term( $category, 'product_cat', $cart_item['data']->id ) ) {
return true;
}
}
}
add_action( 'woocommerce_check_cart_items', 'restrict_members_only_products_to_patrons' );
There isnt any built in feature specific for Woo at this point in time, but something like that can be done by locking shop or checkout pages and functions.
Applying the below code to wrap anything in your theme (or plugin) that provides the functions you need to lock should do it.
Very much of a noob question. I’d like to lock my complete WooCommerce shop as well, so it will be only available for my Patreons. I find myself in the same situation as @docpop in the topic starter.
to lock my shop? I don’t really know where to put this code. The only place where I can add code is the theme editor, but it doesn’t really make sense to me to put it there.
You could use that code where the checkout button or add to cart button is displayed. That would prevent non patrons from adding the product to their cart.
Preventing people from manually adding the product to chart may be more complicated and it may need implementing the same code during checkout to prevent if the person is a non patron. But for most, the earlier will be enough.
Thanks @codebard . That sounds like a good option for sure. Could I find this in the theme editor as well? Which php file would this be? I’m running Neve Pro. Thanks again!
This wouldnt be done via an editor. You normally find the part of the theme where the place which you want to lock is at, and then wrap that part inside the code. Ie, if you want to lock a box inside the header, you check header.php, find the code for that box, and wrap it inside the gate code.
header.php would be in theme’s folder inside wp-content/themes/
However since header.php would get overwritten if you update the theme, creating a child theme and modifying it over it would be better. Googling ‘how to make a child theme’ would bring many tutorials.
I finally made a copy of /wp-content/plugins/woocommerce/templates/single-product/add-to-cart/simple.php into my child theme (found instruction in the php itself!), applied your code and it works!