Buy One Give One - M2P

I want to set up a "buy one give one" situation with membership subscriptions through Membership 2 Pro. Is that possible? If not, any other workaround for this.

And the difference between "For recurring payments coupon is only applied to" "all invoices"
means that if I have a weekly $5 subscription charge and it's on apply to "all" invoices and I give them a $10 coupon.

Then their first two weeks are free? What if they start on the last day before the expiration date?

The start/end date only restrict when the coupon can be applied to a checkout, but the coupon will carry through?

Is there a way for me to make 100 coupons really easily?

  • James Morris
    • WordPress Enthusiast

    Hello stripedrhino

    I hope you are well today.

    This issue has been flagged for our SLS Team (code experts) so that they can dig into this issue further for you. An update will be posted here as soon as more information is available.

    Please keep in mind that this is a bit of a custom code request and as such, may take a little longer to resolve. Our team has to give priority to resolving bugs that are ahead of custom code requests in the queue. Thank you for your understanding.

    Best regards,

    James Morris

  • Adam Czajczyk
    • Support Gorilla

    Hello stripedrhino,

    I hope you're fine today!

    As for coupon start/end dates. Yes, it's a coupon validity date so it means that the coupon itself can only be applied to checkout within the set time frame. Even If the coupon is provided on the last day, the discount is applied as set in coupon settings. After the end date the coupon will no longer be valid but already applied discounts will work.

    As for "buy one, give one". It's not possible out of the box. It can be done manually by an administrator - when you see somebody signing up for a membership, you can manually assign some additional membership to that user.

    However, if that's a specific membership that "gives another one" and it's a specific membership to be given, this code should do the trick:

    function ms_give_bonus_membership() {
    	$base_membership = 1; // id of a membership; only members of this membership will be given an additional membership
    	$bonus_membership = 3; // this is an id of a "bonus" membership that will be given to members
    	$user_id = get_current_user_id(); 
    	//only proceed if not a logged-out visitor
    	if ( $user_id > 0 ) {
    		// get member data
    		$member = MS_Factory::load( 'MS_Model_Member', $user_id);
    		// only proceed if has base_membership but don't have bonus_membership
    		if ( ( $member->has_membership( $base_membership ) ) && ( !( $member->has_membership( $bonus_membership ) ) ) ) {
    			// assign bonus membership
    			$subscription = $member->add_membership( $bonus_membership );
    add_action('wp_head', 'ms_give_bonus_membership');

    You can use it in theme's "functions.php" file or as an MU-plugin (in that case remember to add opening PHP tag before the code: <?php).

    What it does is:

    - first it checks if a current visitor is a logged in user and if so
    - it then checks if that user is a member of the membership that entitles for a "bonus" and a member of a "bonus" membership

    - if the user is a member of both, code does nothing because obviously a user has already been given additional membership;

    - if the user is is a member of the membership that entitles for a "bonus" but not a member of a "bonus" membership - that additional "bonus" membership is assigned to the user.

    I hope that makes sense.

    You can set both memberships in the code directly in these two lines:

    $base_membership = 1; // id of a membership; only members of this membership will be given an additional membership
    $bonus_membership = 3; // this is an id of a "bonus" membership that will be given to members

    For a simple "buy one - get one" scenario with "fixed" memberships that should do the trick. If you need it to be more complex (like e.g. giving different memberships to different members etc) the code above could make a good foundation for further development but custom coding would be required.

    You may consider posting a job request on our "Jobs & Pros" job board (please note: no WPMU DEV staff involved!) if you need assistance with custom coding:

    Best regards,

  • stripedrhino
    • Site Builder, Child of Zeus


    Thank you very much for taking the time to process all these questions, and for your responses and code.

    Great to hear that the start/stop dates only relate to the use of applying the coupon code, and if applied the coupon will carry through those start/stop dates.

    This code is great for those looking to give a bonus membership to existing members, but I was actually hoping to give a coupon to existing members that they could share with their friends. So, if they paid for a billing cycle, then offer them a coupon code that can be applied to a new member during checkout (ie, they give the coupon code to their friend). Ideally, this coupon code would be automagically generated and included in an outbound payment confirmation email. I'll post something in the Job Request board...

    But, what about creating the coupons themselves? What if I wanted to create 100 one-time-use coupon codes? Is there a quick/easy way to do this?


  • stripedrhino
    • Site Builder, Child of Zeus

    Adam Czajczyk,

    I've also noticed that I do not have the option to limit a coupon code to a '1 time use' per email address/user. Granted they can only use a code if they are signing up for the membership, but they could hypothetically cancel and sign back up. (Although I guess there's no way to know if they just sign up using a second email address...)


  • stripedrhino
    • Site Builder, Child of Zeus

    Adam, another question... for the "apply to all invoices" vs the "apply to first invoice"...

    In my scenario, I have a $5 weekly subscription. I want to give them the first two billing cycles for free. So, do I give them a $10 coupon and say "apply to all"? Will that apply $10 off every billing cycle they ever get, effectively giving them my membership for free, or will the coupon run out after those first two billing cycles, and start charging them on cycle 3?

    If giving them a coupon isn't the way to give them the first two billing cycles for free, how can I accomplish this?



  • Panos
    • SLS

    Hi there stripedrhino ,

    This requirement does require an amount of custom coding. I can try provide some guide for how and when to use the coupon model, but we can't cover the WC requirement as it exceeds the purpose of this forum. You should better hire a developer for this.

    As for creating custom coupon, first I'd like to mention that you can create your coupon from admin as Adam mentioned and simply share it with your members. I understand that you want a way to have custom coupon for each member though. You may end up having tons of coupons this way, however here is an approach you can use as a guide:

    add_action( 'ms_invoice_paid', function( $invoice, $subscription ){
    	$member 			= $subscription->get_member();
    	//$membership 		= $subscription->get_membership();
    	$coupon_id 			= get_user_meta( $member->id, 'ms_specific_coupon', true );
    	if( ! empty( $coupon_id ) ) {
    	$length 			= 4;
    	$memberships 		= array( 0 ); // ) for 'Any' membership OR specify membersip ids like <code>array( 5193, 6573, .... );</code>
    	$discount 			= 5;
    	$duration 			= 'once'; // OR 'always'
    	$discount_type 		= 'value';
    	$start_date 		= date( 'Y-m-d' );
    	$coupon_name 		= 'MSC' . substr(str_shuffle(str_repeat($x='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil($length/strlen($x)) )),1,$length);
    	$fields = array(
    		'code' 				=> $coupon_name,
    		'discount' 			=> $discount,
    		'duration' 			=> $duration,
    		'discount_type' 	=> $discount_type,
    		'start_date' 		=> $start_date,
    		'expire_date' 		=> '', // Empty does not expire
    		'membership_id' 	=> $memberships,
    		'max_uses' 			=> 0, //0 sets to unlimited,
    	// Use 0 as coupon id
    	$coupon = MS_Factory::load( 'MS_Addon_Coupon_Model', 0 );
    	foreach ( $fields as $field => $value ) {
    		$coupon->$field = $value;
    	update_user_meta( $member->id, 'ms_specific_coupon', $coupon->id );
    }, 20, 2 );

    If member doesn't have a coupon already set in his meta, it should create a new coupon and save it in the user's ms_specific_coupon meta field.

    You can edit the args of the coupon as you wish by changing the duration, value value_type and memberships as described in the comments.

    Now you have a new coupon created and the coupon id is stored in that user's meta. You can display that coupon information to that member in admin or his ms account. Followinf is a simple example on how to show it in the ms account page:

    add_filter( 'ms_shortcode_account', function( $html, $data ){
    	$member 	= MS_Model_Member::get_current_member();
    	$coupon_id 	= get_user_meta( $member->id, 'ms_specific_coupon', true );
    	if ( $coupon_id && ! empty( $coupon_id ) ) {
    		$coupon = MS_Factory::load( 'MS_Addon_Coupon_Model', $coupon_id );
    		$coupon_html = "<div>";
    			$coupon_html .= "<p>Hey there {$member->name}. You can share coupon <code>{$coupon->code}</code> with your friends so they get a 5$ discount!!</p>";
    		$coupon_html .= "</div>";
    		$html = $coupon_html . $html;
    	return $html;
    }, 20, 2 );

    Hope this helped :slight_smile:

    Kind regards!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.