Skip to content
Browse files

NEW: Introduced Membership CheckoutStep for choosing to become a memb…

…er, logging in, or proceeding as a guest. Added corresponding templates.

Updated OrderForm to be consistent with new membership API.
  • Loading branch information...
1 parent bfbc28f commit f523a10bb3a1e71bb2b2e794a9179d3d0fcb91d2 @jedateach jedateach committed
View
77 code/forms/OrderForm.php
@@ -7,14 +7,6 @@
*/
class OrderForm extends Form {
- //optional for user to become a member
- protected static $user_membership_optional = false;
- static function set_user_membership_optional($optional = true){self::$user_membership_optional = $optional;}
-
- //all users must become members if true, or won't become members if false
- protected static $force_membership = true;
- static function set_force_membership($force = false){self::$force_membership = $force;}
-
//actions that don't need validation
protected $validactions = array(
'useDifferentShippingAddress',
@@ -189,7 +181,7 @@ function useDifferentShippingAddress($data, $form, $request) {
$order->SeparateBillingAddress = true;
$order->write();
$this->saveDataToSession($data);
- Director::redirectBack();
+ Controller::curr()->redirectBack();
}
/**
@@ -200,7 +192,7 @@ function useMemberShippingAddress($data, $form, $request) {
$order->SeparateBillingAddress = false;
$order->write();
$this->saveDataToSession($data);
- Director::redirectBack();
+ Controller::curr()->redirectBack();
}
function updateShippingCountry($data, $form, $request) {
@@ -211,7 +203,7 @@ function updateShippingCountry($data, $form, $request) {
if(Director::is_ajax()){
return "success";
}
- Director::redirectBack();
+ Controller::curr()->redirectBack();
}
/**
@@ -227,37 +219,22 @@ function updateShippingCountry($data, $form, $request) {
* @param HTTPRequest $request Request object for this action
*/
function processOrder($data, $form) {
+ $checkout = Checkout::get();
$this->saveDataToSession($data); //save for later if necessary ...shouldn't technically be needed, if order is being written?
- $cart = ShoppingCart::getInstance();
+ $cart = ShoppingCart::singleton();
$order = $cart->current();
$form->saveInto($order);
- //membership
- $member = Member::currentUser();
- if(!$member){
- if(self::$user_membership_optional){
- if($this->userWantsToBecomeMember($data,$form)){
- $member = ShopMember::ecommerce_create_or_merge($data);
- }
- }elseif(self::$force_membership){
- $member = ShopMember::ecommerce_create_or_merge($data); //create member
- }
- }else{
- //TODO: make this configurable
- $member = ShopMember::ecommerce_create_or_merge($data); //merge data
- }
- //if they are a member, or if they have filled out the member fields (password, save my details)
- // Create new OR update logged in {@link Member} record
- if($member === false) {
- $form->sessionMessage(
- _t('OrderForm.MEMBEREXISTS', 'Sorry, a member already exists with that email address.
- If this is your email address, please log in first before placing your order.'.
- ' <a href="Security/lostpassword">Recover password.</a>'
- ),
- 'bad'
- );
- Director::redirectBack();
- return false;
+ $member = Member::currentUser();
+ if(!$member){
+ $member = new DataObject(); //dummy dataobject to handle the goodness of $form->saveInto
+ $form->saveInto($member);
+ $member = $checkout->createMembership($member->toMap());
+ if(!$member){
+ $form->sessionMessage($checkout->getMessage(),$checkout->getMessageType());
+ Controller::curr()->redirectBack();
+ return;
+ }
}
//save addresses
@@ -276,7 +253,7 @@ function processOrder($data, $form) {
$processor = OrderProcessor::create($order);
if(!$processor->placeOrder()){
$form->sessionMessage($processor->getError(), 'bad');
- Director::redirectBack();
+ Controller::curr()->redirectBack();
return false;
}
$cart->clear();
@@ -295,7 +272,7 @@ function processOrder($data, $form) {
$payment = $processor->createPayment($paymentClass);
if(!$payment){
$form->sessionMessage($processor->getError(), 'bad');
- Director::redirect($order->Link());
+ Controller::curr()->redirect($order->Link());
return false;
}
@@ -312,7 +289,7 @@ function processOrder($data, $form) {
if($result->isSuccess()) {
$processor->sendReceipt();
}
- Director::redirect($payment->ReturnURL);
+ Controller::curr()->redirect($payment->ReturnURL);
return true;
}
@@ -423,5 +400,23 @@ function forTemplate(){
Requirements::customScript($script);
return $form;
}
+
+ //optional for user to become a member
+ protected static $user_membership_optional = false;
+ /*
+ * @deprecated
+ */
+ static function set_user_membership_optional($optional = true){
+ self::$user_membership_optional = $optional;
+ }
+
+ //all users must become members if true, or won't become members if false
+ protected static $force_membership = true;
+ /*
+ * @deprecated
+ */
+ static function set_force_membership($force = false){
+ Checkout::$membership_required = $force;
+ }
}
View
92 code/steppedcheckout/CheckoutStep_Membership.php
@@ -0,0 +1,92 @@
+<?php
+
+/**
+ * Login, sign-up, or proceed as guest
+ */
+class CheckoutStep_Membership extends CheckoutStep{
+
+ static $allowed_actions = array(
+ 'membership',
+ 'MembershipForm',
+ 'LoginForm',
+ 'createaccount',
+ 'CreateAccountForm'
+ );
+
+ static $skip_if_logged_in = true;
+
+ function membership(){
+ //if logged in, then redirect to next step
+ if(self::$skip_if_logged_in && Member::currentUser()){
+ Director::redirect($this->NextStepLink());
+ return;
+ }
+ return $this->owner->customise(array(
+ 'Form' => $this->MembershipForm(),
+ 'LoginForm' => $this->LoginForm()
+ ))->renderWith(array("CheckoutPage_membership","CheckoutPage","Page")); //needed to make rendering work on index
+ }
+
+ function MembershipForm(){
+ $fields = new FieldSet();
+ $actions = new FieldSet(
+ new FormAction("createaccount","Create an Account"),
+ new FormAction("guestcontinue","Continue as Guest")
+ );
+ $form = new Form($this->owner,'MembershipForm',$fields,$actions);
+ $this->owner->extend('updateMembershipForm', $form);
+ return $form;
+ }
+
+ function guestcontinue(){
+ $this->owner->redirect($this->NextStepLink());
+ }
+
+ function LoginForm(){
+ $form = new MemberLoginForm($this->owner,'LoginForm');
+ $this->owner->extend('updateLoginForm', $form);
+ return $form;
+ }
+
+ function createaccount($requestdata){
+ if(Member::currentUser()){ //we shouldn't create an account if already a member
+ Director::redirect($this->NextStepLink());
+ return;
+ }
+ if(!($requestdata instanceof SS_HTTPRequest)){ //using this function to redirect, and display action
+ Director::redirect($this->NextStepLink('createaccount'));
+ return;
+ }
+ return array(
+ 'Form' => $this->CreateAccountForm()
+ );
+ }
+
+ function CreateAccountForm(){
+ $fields = CheckoutFieldFactory::singleton()->getMembershipFields();
+ $actions = new FieldSet(
+ new FormAction('docreateaccount','Create New Account')
+ );
+ $validator = new RequiredFields(array_keys($fields->dataFields())); //require all fields
+ return new Form($this->owner,"CreateAccountForm",$fields,$actions, $validator);
+ }
+
+ function docreateaccount($data, Form $form){
+ $checkout = Checkout::get();
+ //we want to make use the goodness of $form->saveInto, so we need to use a dummy DataObject
+ $data = new DataObject();
+ $form->saveInto($data);
+ $member = $checkout->createMembership($data->toMap());
+ //check member was created, else send back error
+ if(!$member){
+ $form->sessionMessage($checkout->getMessage(), $checkout->getMessageType());
+ Controller::curr()->redirectBack();
+ return;
+ }
+ $member->write();
+ $member->logIn(); //log in member before continuing
+ Controller::curr()->redirect($this->NextStepLink());
+ return;
+ }
+
+}
View
2 code/steppedcheckout/CheckoutStep_Summary.php
@@ -27,6 +27,8 @@ function ConfirmationForm(){
new FormAction("place","Confirm and Pay")
);
//TODO: validation - check if order can be placed via OrderProcessor
+ //make sure payment type exists & is allowed
+ //terms conditions required
return new Form($this->owner,"ConfirmationForm",$fields,$actions);
}
View
12 templates/Layout/CheckoutPage_createaccount.ss
@@ -0,0 +1,12 @@
+<% require themedCSS(checkout) %>
+<h1 class="pageTitle">$Title</h1>
+<div id="Checkout">
+ <div class="typography">
+ <% if Content %>
+ $Content
+ <% end_if %>
+ </div>
+ <div class="createaccount">
+ $Form
+ </div>
+</div>
View
16 templates/Layout/CheckoutPage_membership.ss
@@ -0,0 +1,16 @@
+<% require themedCSS(checkout) %>
+<h1 class="pageTitle">$Title</h1>
+<div id="Checkout">
+ <div class="typography">
+ <% if Content %>
+ $Content
+ <% end_if %>
+ </div>
+ <div class="membership">
+ $Form
+ </div>
+ <div class="login">
+ <h2>Log In</h2>
+ $LoginForm
+ </div>
+</div>

0 comments on commit f523a10

Please sign in to comment.
Something went wrong with that request. Please try again.