How to create a free-shipping membership site for WooCommerce.

One of the things I love about designing with WordPress is that there are so many people using it that you can usually find a solution to any problem you have. Every once in a while that solution is hard to find.

The problem: Create a WooCommerce Membership site with free shipping for members.

The folks at DivineRise.com want to create an Amazon Prime-like membership site where members enjoy free shipping. We already created the site using WooCommerce along with WooCommerce Subscriptions and Memberships plugins.  The Memberships plugin allows members to get a percentage or dollar amount discount on products, but doesn’t offer a solution to discount the shipping.

After scouring the internet for a solution, I found a couple of user role-based code snippets that would help us accomplish this. The problem with a role-based solution is that we want to offer free-shipping to customers with a membership, but not to customers without a membership. The key here is that both have the same user role: customer.

The Memberships plugin creates a new field: Active Memberships. This is the tag that we want to apply our code snippet to.

WooCommerce Users 

The Solution: Hide ‘Free Shipping’ shipping method from customers whose ‘Active Memberships’ tag is empty.

We got a huge assist from our studio-mates at Technical Imagination to assemble this snippet:

 

/* adds free shipping to members*/

add_filter( 'woocommerce_shipping_zone_shipping_methods', 'custom_shipping_methods' );

function custom_shipping_methods( $available_methods ) {
	get_currentuserinfo();
	global $current_user;
	
	if ($current_user->ID) {
		
		$active_memberships = wc_memberships_get_user_active_memberships();
		if ( empty( $active_memberships )  && ! is_admin() ) {
			foreach ( $available_methods as $key => $method ) {
				// show free shipping for active members
				if ( 'free_shipping' === $method->id  ) {
					unset( $available_methods[ $key ] );
				}
			}
		}
	} else {
		foreach ( $available_methods as $key => $method ) {
			// show free shipping for active members
			if ( 'free_shipping' === $method->id  && ! is_admin()) {
				unset( $available_methods[ $key ] );
			}
		}
	}
	return $available_methods;
}

To Implement: copy the code and put it into your child theme’s functions.php file above the >? line on the bottom.

Leave a Comment

Your email address will not be published. Required fields are marked *