WordPress.org

Support

Support » Plugins and Hacks » WooCommerce » How to set a checkbox to checked as default

How to set a checkbox to checked as default

  • I want to change

    <?php if ( ! is_user_logged_in() && $checkout->enable_signup ) : ?>
    
    	<?php if ( $checkout->enable_guest_checkout ) : ?>
    
    		<p class="form-row form-row-wide">
    			<input class="input-checkbox" id="createaccount" <?php checked($checkout->get_value('createaccount'), true) ?> type="checkbox" name="createaccount" value="1"/> <label for="createaccount" class="checkbox"><?php _e( 'Create an account?', 'woocommerce' ); ?></label>
    		</p>

    to

    <?php if ( ! is_user_logged_in() && $checkout->enable_signup ) : ?>
    
    	<?php if ( $checkout->enable_guest_checkout ) : ?>
    
    		<p class="form-row form-row-wide">
    			<input class="input-checkbox" id="createaccount" <?php checked($checkout->get_value('createaccount'), true) ?> type="checkbox" name="createaccount" value="1" checked="checked"/> <label for="createaccount" class="checkbox"><?php _e( 'Create an account?', 'woocommerce' ); ?></label>
    		</p>

    with the help of a functions.php to survive WooCommerce updates (see the different checked=”checked” part).
    How can I do that?
    Thanks

    http://wordpress.org/extend/plugins/woocommerce/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Get rid of this aspect:

    <?php checked($checkout->get_value('createaccount'), true) ?>

    What do you mean?
    I have only pasted the original code which came with WooCommerce 2.0.9 and I am looking for a persistent solution to make the checkbox to create a new account checked as default even after WooCommerce updates.

    Modify the billing form template ( form-billing.php ) . Move a copy to your theme, then edit it. No function required. See the WooCommerce documentation for how to copy templates to your theme directory.

    Isn’t it possible with a functions.php solution?
    Would be one file less to create and easier to adapt to changes.

    Either way you’re likely to be subject to adaptations at some point or other down the road.

    I have added a pull request in GitHub now: https://github.com/woothemes/woocommerce/pull/3098

    WooCommerce (at least in version 2.1.7) has an apply-filters tag in that checked statement now:
    (from default form-billing.php):
    <input class="input-checkbox" id="createaccount" <?php checked( ( true === $checkout->get_value( 'createaccount' ) || ( true === apply_filters( 'woocommerce_create_account_default_checked', false ) ) ), true) ?> type="checkbox" name="createaccount" value="1" /> <label for="createaccount" class="checkbox"><?php _e( 'Create an account?', 'woocommerce' ); ?></label>

    So, simply adding this statement in functions.php should work:
    add_filter( 'woocommerce_create_account_default_checked', function( $isChecked) { return true; } );

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to set a checkbox to checked as default’ is closed to new replies.
Skip to toolbar