How to create a Patreon-only section of my Woocommerce site?


#1

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.


#2

That’s a bit advanced usage, indeed.

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:


#3

That’s a great set of suggestions, thanks!

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.


#4

That also could be done by the same method:

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.


#5

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' );

#6

Yes, that would work. Good one!