custom checkbox at checkout
-
Hi
I am trying to add a custom checkbox that is optional but checked by default, i was hoping to put it right before i agree to the terms and conditions but i am not sure how to do that.
for now i was able to figure out how to put it right after the order notes, but i cant figure out how to make the checkout to be cheched by default. i have the following code:function cw_custom_checkbox_fields( $checkout ) {
echo ‘<div class=”cw_custom_class”><b>’.__(‘Leave at my door if i am not around’).'</b>’;
woocommerce_form_field( ‘custom_checkbox’, array(
‘type’ => ‘checkbox’,
‘label’ => __(‘By selecting this options you accept full responsibility for your order after it has been delivered unattended, including any loss due to theft or damage due to temperature sensitivity. If you choose not to select this option and we are unable to deliver, there will be a $10 restocking fee charge’),
‘required’ => false,
), $checkout->get_value( ‘custom_checkbox’ ));
echo ‘</div>’;
}
add_action(‘woocommerce_after_order_notes’, ‘cw_custom_checkbox_fields’);
add_action(‘woocommerce_checkout_process’, ‘cw_custom_process_checkbox’);
function cw_custom_process_checkbox() {
global $woocommerce;
if (!$_POST[‘custom_checkbox’])
wc_add_notice( __( ‘Notification message.’ ), ‘error’ );
}
add_action(‘woocommerce_checkout_update_order_meta’, ‘cw_checkout_order_meta’);
function cw_checkout_order_meta( $order_id ) {
if ($_POST[‘custom_checkbox’]) update_post_meta( $order_id, ‘checkbox name’, esc_attr($_POST[‘custom_checkbox’]));
}another problem i am having it says (OPTIONAL) right after you will be charged a restocking fee, which client can get confuse that its option to pay that fee. so i added this:
add_filter( ‘woocommerce_form_field’ , ‘remove_checkout_optional_fields_label’, 10, 4 );
function remove_checkout_optional_fields_label( $field, $key, $args, $value ) {
// Only on checkout page
if( is_checkout() && ! is_wc_endpoint_url() ) {
$optional = ‘ <span class=”optional”>(‘ . esc_html__( ‘optional’, ‘woocommerce’ ) . ‘)</span>’;
$field = str_replace( $optional, ”, $field );
}
return $field;
}
but now all words optional including for company name is gone!
can someone help me fix my code?
- The topic ‘custom checkbox at checkout’ is closed to new replies.