Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » How to have payed Members only pages?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1332 Views
  • purplespider
    Avatar
    Community Member
    81 Posts

    How to have payed Members only pages? Link to this post

    I'm really excited about using SilverStripe for my web development projects, but I have one site though which I'm not sure how easy it would be to accomplish in SS.

    It is an educational website, where you have to pay a yearly/monthly fee to view most of the pages.

    The main aspect I am worried about is how to deal with "members" in SilverStripe. I need the ability:

    - for members to register and pay and have immediate access to members only pages
    - need to be able to have different categories of membership, some will get it cheaper, some for free etc

    Can someone please point me in the right direction for how I could accomplish this in SilverStripe? Is it possible without too much extra coding?

    Many Thanks!
    James

  • carlos
    Avatar
    Community Member
    41 Posts

    Re: How to have payed Members only pages? Link to this post

    Hi

    Sure you can do this with SS.

    - First create a registration form (we will need to create the logic to connect to a online payment gateway, or use payment module).

    - Once you sorted payment, you need to create a new member and save it into a group (you can create different groups).

    function registerAction($data,$form) { //you call this function when user submit registation form (or after you check payment)
          $member=DataObject::get_one('Member',"Email = '" . mysql_real_escape_string($data['Email']) . "'"); //check if the email is in the DB already
          if(!$member) { // if email is not in DB create a new member
             $member = new Member();
             $form->saveInto($member); //insert new member value from the form
             $member->RegistrationHash = md5(uniqid(rand(), true));
             $member->write(); //save new member
          }
    $member->Groups()->add('myGroup');

    - Add a check box to your pages so you can choose if the page is members only.
    Page.php

    function getCMSFields() {
           $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.Main", new CheckboxField('MemberOnly', 'For member only?'), 'Content');
    ...

    - create a function in Page.php to check if the page is members only and check if the visitor is logged in.
    Page.php

    function canView($member = null) {   // this function gets called when you load the page.
          if($this->MemberOnly) {
             $member = Member::currentUser();
             if (!$member) return false;
          }
          
          return parent::canView($member);
       }

  • purplespider
    Avatar
    Community Member
    81 Posts

    Re: How to have payed Members only pages? Link to this post

    Amazing! I shall give this a go, thank you very much!

    1332 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.