Magento – Show fields in register form of checkout onepage when the user is not guest

magento-1.9onepage-checkoutregister

I added a custom attribute to customer entity in my Magento store and successfully displayed it in the registration page.
Now I want to display it in the registration form of the onepage checkout. I tried to modifiy the persistent/checkout/onepage/billing.phtml to add my new field but adding it to this file shows the new field also when a user checkouts as guest.

How can I show it only when a new user is registering in checkout onepage?

Best Answer

Magento does this for the remember me option on billing form via javascript.

It simply checks if the field login:guest has been selected and then hides/shows the appropriate fields. In your case you could do a similar approach and simply replace the field that is hidden.

if ($('onepage-guest-register-button')) {
    Event.observe($('onepage-guest-register-button'), 'click', function(event) {
        var billingRememberMe = $('co-billing-form').select('#remember-me-box');
        if (billingRememberMe.length > 0) {
            if ($('login:guest') && $('login:guest').checked) {
                billingRememberMe[0].hide();
            } else if ($('login:register') && ($('login:register').checked || $('login:register').type == 'hidden')) {
                billingRememberMe[0].show();
            }
        }
    });
}