Helen_zs
Helen_zs

Reputation: 85

How I set a checkbox to be always checked in WooCommerce product admin?

How to set a checkbox to be always checked? I'm using a plugin "Woocommerce product fee" that ads a fee to products. It has a checkbox that, when is checked it calculates the fees for every product added in the cart. What I want to achieve is to set this checkbox to be always checked for every product but I can't find the right way. I added $checked but nothing happens...

This is an extract of my code:

    // Check Box - Fee Multiply Option
    woocommerce_wp_checkbox( array( 
        'id'=> 'product-fee-multiplier[' . $variation->ID . ']', 
        'label' => __('Multiply Fee by Quantity', 'woocommerce-product-fees' ),
        'value' => get_post_meta( $variation->ID, 'product-fee-multiplier', true ), 
        'wrapper_class' => "product-fee-multiplier" , 
        'required'  => true
    ), $checked );

    do_action( 'wcpf_add_variation_settings' );

}

public function save_variation_settings_fields( $post_id ) {
    $another_field_updated = false;
    // Text Field - Fee Name
    $product_fee_name_text_field = $_POST['product-fee-name'][ $post_id ];
        if( ! empty( $product_fee_name_text_field ) || get_post_meta( 
        $post_id, 'product-fee-name', true ) != '' ) {
            update_post_meta( $post_id, 'product-fee-name', esc_attr( $product_fee_name_text_field ) );
            $another_field_updated = true;
        }

Upvotes: 3

Views: 7378

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253959

When using Woocommerce woocommerce_wp_checkbox() form field function, if you wish to have the checkbox always checked by default you will use 'cbvalue' array argument like:

// Check Box - Fee Multiply Option
woocommerce_wp_checkbox( array( 
    'id'            => 'product-fee-multiplier[' . $variation->ID . ']', 
    'label'         => __('Multiply Fee by Quantity', 'woocommerce-product-fees' ),
    'value'         => get_post_meta( $variation->ID, 'product-fee-multiplier', true ), 
    'cbvalue'       => get_post_meta( $variation->ID, 'product-fee-multiplier', true ), 
    'wrapper_class' => "product-fee-multiplier" , 
    'required'      => true
), $checked );

do_action( 'wcpf_add_variation_settings' );

This will allow you to have the targeted checkbox always checked.


Since woocommerce 3, Here is an easy complete working example for "product" post type:

// Displaying quantity setting fields on admin product pages
add_action( 'woocommerce_product_options_pricing', 'add_custom_field_product_options_pricing' );
function add_custom_field_product_options_pricing() {
    global $product_object;

    echo '</div><div class="options_group">';

    $values = $product_object->get_meta('_cutom_meta_key');

    woocommerce_wp_checkbox( array( // Checkbox.
        'id'            => '_cutom_meta_key',
        'label'         => __( 'Custom label', 'woocommerce' ),
        'value'         => empty($values) ? 'yes' : $values,
        'description'   => __( 'Enable this to make something.', 'woocommerce' ),
    ) );
}

// Save quantity setting fields values
add_action( 'woocommerce_admin_process_product_object', 'save_custom_field_product_options_pricing' );
function save_custom_field_product_options_pricing( $product ) {
    $product->update_meta_data( '_cutom_meta_key', isset($_POST['_cutom_meta_key']) ? 'yes' : 'no' );
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Upvotes: 7

Nik Horse
Nik Horse

Reputation: 183

If You try to read docs, You can see, that function woocommerce_wp_checkbox() has inside different parameters and use checked() function inside to. So, it add checked, if value = cbvalue. Also, You can add custom by using param custom_attributes. So, you have two ways:

woocommerce_wp_checkbox( array( 
    'id'=> 'product-fee-multiplier[' . $variation->ID . ']', 
    'label' => __('Multiply Fee by Quantity', 'woocommerce-product-fees' ),
    'value' => get_post_meta( $variation->ID, 'product-fee-multiplier', true ), 
    'cbvalue' => get_post_meta( $variation->ID, 'product-fee-multiplier', true ), 
    'wrapper_class' => "product-fee-multiplier" , 
    'required'  => true
));

OR:

woocommerce_wp_checkbox( array( 
    'id'=> 'product-fee-multiplier[' . $variation->ID . ']', 
    'label' => __('Multiply Fee by Quantity', 'woocommerce-product-fees' ),
    'value' => get_post_meta( $variation->ID, 'product-fee-multiplier', true ), 
    'custom_attributes' => 'checked',
    'wrapper_class' => "product-fee-multiplier" , 
    'required'  => true
));

Upvotes: -1

Related Questions