latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Helpy with custom capabilities

Home Forums Community Forum Helpy with custom capabilities

This topic contains 2 replies, has 2 voices. Last updated by  Cristián Lávaque 4 years ago.

Topic Author Topic
Posted: Saturday Jan 5th, 2013 at 10:18 pm #36366

Okay, so I’ve got this all pretty much figured out. I must admit that I know only enough about PHP to get me by. So if You see something not done correctly, I would be happy to hear your thoughts.

Here is what I’m trying to do:

Using custom capabilities, which are dynamically generated, that will give registered users access to a “course” once they purchase the course.

Custom capabilities are created using the prefix “access_s2member_ccap_xxx” – where xxx can be whatever you want. I know that the paypal button sends this field to paypal so that s2member knows the item was purchased and therefore grants access to that person.

This is a lot of code I am about to post. My problem, I think lies on this on this function –

if (current_user_can(access_s2member_ccap_$cnumbr)){

This should be located around line 6…..I have changed the paypal email and the domain name for security reasons. So please don’t say that the issue is my domain name or paypal email. I have tested the setup by purchasing the course with a test account, and then replacing $cnumbr with text, which is “l5513” so the output should be “access_s2member_ccap_l5513” which I know it works because when I do it with text, it works, but when I try to generate it dynamically, it fails.

I’m sure it has to do with my syntax or whatever, I have tried it with every possibility of single quotes and double quotes, but I’ve been staring it at it too long!

Any help is appreciated!

Here is the code!
<?php if (current_user_can('access_s2member_level1')){
    
	echo "<h4>Course Details</h4>";
	
     if (current_user_can(access_s2member_ccap_$cnumbr)){

    //if a user is logged in and has purchased a course we show them the course //
	$cid = get_post_meta($post->ID, 'show_course_ss', true);
	do_shortcode ('[[wpcourse course="'.$cid.'" module_desc="false" show_title="false" show_desc="false"/]');
   
   } else {
	
	$sql = "SELECT * FROM wp_wpcw_courses";
	$result = mysql_query($sql)or die(mysql_error());
	$row = mysql_fetch_array($result);
	$cprice    = $row['course_price'];
	$ctitle    = $row['course_title'];
	$cnumbr    = $row['course_number'];
	$postid = get_the_ID();
	$shows2ppinv = S2MEMBER_VALUE_FOR_PP_INV();
	$s2cuvfppon0 = S2MEMBER_CURRENT_USER_VALUE_FOR_PP_ON0;
	$s2cuvfppon1 = S2MEMBER_CURRENT_USER_VALUE_FOR_PP_ON1;
	$s2cuvfppos0 = S2MEMBER_CURRENT_USER_VALUE_FOR_PP_OS0;
	$s2cuvfppos1 = S2MEMBER_CURRENT_USER_VALUE_FOR_PP_OS1;
	// For some reason custom capabilities in s2member only work if they are lowercase - so this will take care of that lil bugger
	$lowercnumber = strtolower($cnumbr);
	echo "<form action='https://www.paypal.com/cgi-bin/webscr' method='post'>
 <input type='hidden' name='business' value='paypalchanged@forsecurityreasons.com' />
 <input type='hidden' name='cmd' value='_xclick' />
 <!-- Instant Payment Notification & Return Page Details -->
 <input type='hidden' name='notify_url' value='http://example.com/?s2member_paypal_notify=1' />
 <input type='hidden' name='cancel_return' value='http://example.com/' />
 <input type='hidden' name='return' value='http://example.com/?s2member_paypal_return=1' />
 <input type='hidden' name='rm' value='2' />
 <!-- Configures Basic Checkout Fields -->
 <input type='hidden' name='lc' value='' />
 <input type='hidden' name='no_shipping' value='1' />
 <input type='hidden' name='no_note' value='1' />
 <input type='hidden' name='custom' value='example.com' />
 <input type='hidden' name='currency_code' value='USD' />
 <input type='hidden' name='page_style' value='paypal' />
 <input type='hidden' name='charset' value='utf-8' />
 <input type='hidden' name='item_name' value='".$ctitle."' />
 <input type='hidden' name='item_number' value='1:".$lowercnumber."' />
 
 <input type='hidden' name='invoice' value='". $shows2ppinv ."' />
 
 <input type='hidden' name='on0' value='". $s2cuvfppon0 ."' />
 <input type='hidden' name='os0' value='". $s2cuvfppos0 ."' />
 <!-- Identifies The Customers IP Address For Tracking -->
 <input type='hidden' name='on1' value='". $s2cuvfppon1 ."' />
 <input type='hidden' name='os1' value='". $s2cuvfppos1 ."' />
 <!-- Controls Modify Behavior At PayPal Checkout -->
 <!-- Customizes Prices, Payments & Billing Cycle -->
 <input type='hidden' name='amount' value='".$cprice."' />

 <input type='image' src='https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif' style='width:auto; height:auto; border:0;' alt='PayPal' />
</form>";
	
	}
    
 } else {

 //if a user is NOT logged in - they can neither purchase a course and they cannot view the course - regardless
	echo "<div class='cqd-notice rounded'>Please <a href='/register/'>Register</a> for an account or <a href='/login/'>Login</a> with an existing an account to <strong>enroll</strong> or <strong>purchase</strong> this course!</div>";
	
	
} ?>

List Of Topic Replies

Viewing 2 replies - 1 through 2 (of 2 total)
Author Replies
Author Replies
Posted: Sunday Jan 6th, 2013 at 1:51 am #36370

I got it working

if (current_user_can("access_s2member_ccap_" . $lowercnumber)){

Here is the complete working code (in case someone needs to do something like this

Declare Variables at top of template file —->

// declare variables //
	$cid = get_post_meta($post->ID, 'show_course_ss', true);
	$sql = "SELECT * FROM wp_wpcw_courses";
	$result = mysql_query($sql)or die(mysql_error());
	$row = mysql_fetch_array($result);
	$cprice    = $row['course_price'];
	$ctitle    = $row['course_title'];
	$cnumbr    = $row['course_number'];
	$postid = get_the_ID();
	$shows2ppinv = S2MEMBER_VALUE_FOR_PP_INV();
	$s2cuvfppon0 = S2MEMBER_CURRENT_USER_VALUE_FOR_PP_ON0;
	$s2cuvfppon1 = S2MEMBER_CURRENT_USER_VALUE_FOR_PP_ON1;
	$s2cuvfppos0 = S2MEMBER_CURRENT_USER_VALUE_FOR_PP_OS0;
	$s2cuvfppos1 = S2MEMBER_CURRENT_USER_VALUE_FOR_PP_OS1;
	// For some reason custom capabilities in s2member only work if they are lowercase - so this will take care of that lil bugger
	$lowercnumber = strtolower($cnumbr);
	

Use this code where you would like data to appear in template file –>

<?php if (current_user_can('access_s2member_level1')){
    
	echo "<h4>Course Details</h4>";
	
     if (current_user_can("access_s2member_ccap_" . $lowercnumber)){
	 

    //if a user is logged in and has purchased a course we show them the course //
	
	echo do_shortcode ('[wpcourse course="'.$cid.'" module_desc="false" show_title="false" show_desc="false"/]');
   
   } else {
	

	echo "<form action='https://www.paypal.com/cgi-bin/webscr' method='post'>
 <input type='hidden' name='business' value='mypaypal@mydomain.com' />
 <input type='hidden' name='cmd' value='_xclick' />
 <!-- Instant Payment Notification & Return Page Details -->
 <input type='hidden' name='notify_url' value='http://example.com/?s2member_paypal_notify=1' />
 <input type='hidden' name='cancel_return' value='http://example.com/' />
 <input type='hidden' name='return' value='http://example.com/?s2member_paypal_return=1' />
 <input type='hidden' name='rm' value='2' />
 <!-- Configures Basic Checkout Fields -->
 <input type='hidden' name='lc' value='' />
 <input type='hidden' name='no_shipping' value='1' />
 <input type='hidden' name='no_note' value='1' />
 <input type='hidden' name='custom' value='example.com' />
 <input type='hidden' name='currency_code' value='USD' />
 <input type='hidden' name='page_style' value='paypal' />
 <input type='hidden' name='charset' value='utf-8' />
 <input type='hidden' name='item_name' value='".$ctitle."' />
 <input type='hidden' name='item_number' value='1:".$lowercnumber."' />
 
 <input type='hidden' name='invoice' value='". $shows2ppinv ."' />
 
 <input type='hidden' name='on0' value='". $s2cuvfppon0 ."' />
 <input type='hidden' name='os0' value='". $s2cuvfppos0 ."' />
 <!-- Identifies The Customers IP Address For Tracking -->
 <input type='hidden' name='on1' value='". $s2cuvfppon1 ."' />
 <input type='hidden' name='os1' value='". $s2cuvfppos1 ."' />
 <!-- Controls Modify Behavior At PayPal Checkout -->
 <!-- Customizes Prices, Payments & Billing Cycle -->
 <input type='hidden' name='amount' value='".$cprice."' />

 <input type='image' src='https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif' style='width:auto; height:auto; border:0;' alt='PayPal' />
</form>";
	
	}
    
 } else {

 //if a user is NOT logged in - they can neither purchase a course and they cannot view the course - regardless
	echo "<div class='cqd-notice rounded'>Please <a href='/register/'>Register</a> for an account or <a href='/login/'>Login</a> with an existing an account to <strong>enroll</strong> or <strong>purchase</strong> this course!</div>";
	
	
} ?>
Posted: Monday Jan 7th, 2013 at 8:06 pm #36510

Cool! Glad you got it working, Tina. Thanks for the update and for sharing your solution. :)

Viewing 2 replies - 1 through 2 (of 2 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.