Magento – Checkout register if password field got value otherwise guest checkout

checkoutonepage-checkoutonestepcheckout

I want to display the password field always in the billing step of the checkout.
We removed the fist step in the checkout.

In this billing step we want that if a customer enters a password, it will create a account. If the customer leaves the input fields empty, it will checkout as a guest.

In billing.phtml I currently got this line above the password input fields:

<?php if($this->helper('skipstep1')->isSkipEnabled() && $this->getQuote()->isAllowedGuestCheckout()): ?>
            <li class="fields">
            <div class="field">
                            <label class="account-aanmaken-checkout" for="login:register"><?php echo $this->__('Register with us for future convenience') ?></label>
                <div class="input-box checkout-account">
                <input type="checkbox" class="checkbox" name="login[register]" id="login:register" value="1" title="<?php echo $this->__('Register') ?>" onclick="toggleRegister(this)"<?php if (Mage::getStoreConfig('checkout/skipstep1/default_method')=='register'): ?> checked="checked"<?php endif ?>/>
                </div>
            </li>
            <?php endif ?>

Default code for the password field is this:

        <li class="fields" id="register-customer-password">
            <div class="field">
                <label for="billing:customer_password" class="required"><em>*</em><?php echo $this->__('Password') ?></label>
                <div class="input-box">
                    <input type="password" name="billing[customer_password]" id="billing:customer_password" title="<?php echo $this->__('Password') ?>" class="input-text required-entry validate-password" />
                </div>
            </div>
            <div class="field">
                <label for="billing:confirm_password" class="required"><em>*</em><?php echo $this->__('Confirm Password') ?></label>
                <div class="input-box">
                    <input type="password" name="billing[confirm_password]" title="<?php echo $this->__('Confirm Password') ?>" id="billing:confirm_password" class="input-text required-entry validate-cpassword" />
                </div>
            </div>
        </li>

Controller:

<?php if (Mage::helper('skipstep1')->isSkipEnabled()): ?>
<script type="text/javascript">
function toggleRegister(checkbox) {
    // If registration is checked, or the customer has no choice => register
    if (!checkbox || checkbox.checked) {
        checkout.method = 'register';
        new Ajax.Request(
            checkout.saveMethodUrl,
            {method: 'post', onFailure: checkout.ajaxFailure.bind(checkout), parameters: {method:'register'}}
        );
        Element.show('register-customer-password');
        if ($('remember-me-box')) {
            $('remember-me-box').show();
        }
    // If the customer has a choice, and chose not to register => checkout as guest
    } else {
        checkout.method = 'guest';
        new Ajax.Request(
            checkout.saveMethodUrl,
            {method: 'post', onFailure: checkout.ajaxFailure.bind(checkout), parameters: {method:'guest'}}
        );
        Element.hide('register-customer-password');
        if ($('remember-me-box')) {
            $('remember-me-box').hide();
        }
    }
}

function toggleLogin() {
    $('login-form').toggle();
    $('co-billing-form').toggle();
    $('billing-login-link').toggle();
    $('billing-guest-link').toggle();
}

<?php if (!Mage::getSingleton('customer/session')->isLoggedIn()): ?>
checkout.method = '<?php echo Mage::getStoreConfig('checkout/skipstep1/default_method') ?>';
checkout.gotoSection('billing');
toggleRegister($('login:register'));
<?php endif ?>
<?php if ($this->getMessagesBlock()->getMessageCollection()->count()): // Failed login => message => hide address form / show login ?>
toggleLogin();
<?php endif ?>
</script>
<?php endif ?>

That display a checkbox, if checked the customer can register.
If leave unchecked, the customer will checkout as a guest.
That works great, but how can I modify it that it will work without the checkbox.

How can I modify this?

Best Answer

Edited

First you need to skip checkout method save and need to directly goes to billing steps and there will you save checkout method as guest/register depend on your logic of password.

There are two way to save the checkout method.

  • Rewrite controller:
    • Using Observer

Rewrite Controller:

If you are using default magento onepagecontroller for save billing step data then you add rewrite controller.

<?php
require_once Mage::getModuleDir('controllers', 'Mage_Checkout').DS.'OnepageController.php';
class Amit_Checkout_OnepageController extends Mage_Checkout_OnepageController
{
        public function saveBillingAction()
    {
        if ($this->_expireAjax()) {
            return;
        }
        if ($this->getRequest()->isPost()) {
//            $postData = $this->getRequest()->getPost('billing', array());
//            $data = $this->_filterPostData($postData);

        // put add my code here 
        $CheckoutMethod='guest';
        $PassData = $this->getRequest()->getPost('billing', array());

        if(isset($PassData['customer_password']) && ($PassData['customer_password']!='')):
            $customer_password=$PassData['customer_password'];
            $confirm_password='';
            if(isset($PassData['confirm_password']) && ($PassData['confirm_password']!='')):
            $confirm_password=$PassData['confirm_password'];
            endif;
            /* if passpword match */
            if($confirm_password==$customer_password){
            $CheckoutMethod='register'; 
            }
        endif;
          $this->getOnepage()->saveCheckoutMethod($CheckoutMethod);



            $data = $this->getRequest()->getPost('billing', array());
            $customerAddressId = $this->getRequest()->getPost('billing_address_id', false);

            if (isset($data['email'])) {
                $data['email'] = trim($data['email']);
            }
            $result = $this->getOnepage()->saveBilling($data, $customerAddressId);

            if (!isset($result['error'])) {
                /* check quote for virtual */
                if ($this->getOnepage()->getQuote()->isVirtual()) {
                    $result['goto_section'] = 'payment';
                    $result['update_section'] = array(
                        'name' => 'payment-method',
                        'html' => $this->_getPaymentMethodsHtml()
                    );
                } elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1) {
                    $result['goto_section'] = 'shipping_method';
                    $result['update_section'] = array(
                        'name' => 'shipping-method',
                        'html' => $this->_getShippingMethodsHtml()
                    );

                    $result['allow_sections'] = array('shipping');
                    $result['duplicateBillingInfo'] = 'true';
                } else {
                    $result['goto_section'] = 'shipping';
                }
            }

            $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
        }
    }


}

By Using observer:

On event controller_action_predispatch_checkout_onepage_saveBilling fire an event which will Save checkout method.

config.xml code:

<global>
    <models>
        <custommodule>
            <class>Amit_CustomModule_Model</class>
        </custommodule>
    </models>
</global>
<frontend>
<events>
<controller_action_predispatch_checkout_onepage_saveBilling>
    <observers>
        <captcha>
            <class>amit/observer</class>
            <method>AssignMethod</method>
        </captcha>
    </observers>
</controller_action_predispatch_checkout_onepage_saveBilling>
<events>
</frontend>

Observer code:

Then on Observer save checkout method as guest/register

<?php
class Amit_CustomModule_Model_Observer{

    public function AssignMethod($observer){
    $CheckoutMethod='guest';
    // check customer is loggedin or not 
    if (!Mage::getSingleton('customer/session')->isLoggedIn()){
            $PassData = $this->getRequest()->getPost('billing', array());

            if(isset($PassData['customer_password']) && ($PassData['customer_password']!='')):
                $customer_password=$PassData['customer_password'];
                $confirm_password='';
                if(isset($PassData['confirm_password']) && ($PassData['confirm_password']!='')):
                $confirm_password=$PassData['confirm_password'];
                endif;
                /* if passpword match */
                if($confirm_password==$customer_password){
                $CheckoutMethod='register'; 
                }
            endif;
     }else{
        $CheckoutMethod='register';

     }
      Mage::getSingleton('checkout/type_onepage')->saveCheckoutMethod($CheckoutMethod);
    }
}

On JavaScript set guest as default checkout method for going to billing step then using above two process change the checkout method basic of password field

<?php if (Mage::helper('skipstep1')->isSkipEnabled()): ?>
<script type="text/javascript">
function toggleRegister(checkbox) {


        checkout.method = 'guest';
        new Ajax.Request(
            checkout.saveMethodUrl,
            {method: 'post', onFailure: checkout.ajaxFailure.bind(checkout), parameters: {method:'register'}}
        );
        Element.show('register-customer-password');
        if ($('remember-me-box')) {
            $('remember-me-box').show();
        }
    // If the customer has a choice, and chose not to register => checkout as guest
}

function toggleLogin() {
    $('login-form').toggle();
    $('co-billing-form').toggle();
    $('billing-login-link').toggle();
    $('billing-guest-link').toggle();
}


checkout.gotoSection('billing');
toggleRegister($('login:register'));
<?php if ($this->getMessagesBlock()->getMessageCollection()->count()): // Failed login => message => hide address form / show login ?>
toggleLogin();
<?php endif ?>
</script>
<?php endif ?>