7

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?

2
  • please shoe the controller code..
    – Amit Bera
    Apr 13 '15 at 13:52
  • Edit the question.
    – JGeer
    Apr 13 '15 at 14:01
3
+25

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 ?>     
10
  • So which file do I need to modify and how exactly?
    – JGeer
    Apr 13 '15 at 14:20
  • Put this code on method controller. Where Billi step value is svaed
    – Amit Bera
    Apr 13 '15 at 14:23
  • @AmitBera: Does that controller have a class-name? If so, please add the full classname to your question so it's clear what you mean to do here. From a quick glance this looks like you're editing core-files?
    – hakre
    Apr 13 '15 at 14:27
  • have you used any module for save billing data
    – Amit Bera
    Apr 13 '15 at 14:41
  • No, we do not use any module for saving billing data
    – JGeer
    Apr 13 '15 at 14:42
1

I would say the best method to do this is by overriding the onepagecontroller if it is the controller you are using as suggested by Amit Bera. But if you want to take an easy route you can achieve this in the billing.phtml file itself. This trick is to use a hidden element in place of the check box with the same name as the checkbox. Instead of the onclick event of the checkbox you can use the onblur event of the password field and do the toggleRegister logic accordingly. I understand this will solve your purpose.

EDITED

Please add onblur event in your password field as below,

<input type="password" name="billing[customer_password]" id="billing:customer_password" title="<?php echo $this->__('Password') ?>" class="input-text required-entry validate-password" onblur="toggleRegister(this)" />

Now inside your form add a hidden element like this.

<input type="hidden" name="login[register]" id="login:register" value="0" />

Now replace the toggleRegister() method with the following

EDIT:

function toggleRegister(ele) {
    //Please change the following line
    if($('billing:customer_password').value != ''){
        $('login_register').value = '1';
        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();
        }
    }else{
        $('login_register').value = '0';
        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();
        }
    }
}
17
  • Thanks! Can you give an example of how this code should look like? Because that is the part I can't get done.
    – JGeer
    Apr 14 '15 at 6:36
  • Tried your code, but that did not work. I replaced everything with your code, but then the entire password fields are not displayed.
    – JGeer
    Apr 14 '15 at 14:08
  • I posted this code for your reference. Please don't replace the whole code. Please mark the difference and update your code accordigly. Let me know what happens. Apr 14 '15 at 14:19
  • Please see the edited answer and let me know what happens. Apr 15 '15 at 5:13
  • Thanks for your edit. Do I need to remove the entire code between the line <?php // SKIP STEP 1 START ?> and <?php // SKIP STEP 1 END ?>?. Because when I remove that code and I add your code, the fields are gone.
    – JGeer
    Apr 15 '15 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.