kd7cyu
kd7cyu

Reputation: 15

Add checkbox to WooCommerce product advanced options tab

I am trying to add a checkbox to the product page in WooCommerce. It is being used to tell if a product needs a release form signed on checkout of or not.

This is done in a child theme. The checkbox shows up in the advanced tab for a product which is where I want it but it is not being saved.

Here is the code from my functions.php that creates the checkbox and saves it.

function add_release_checkbox() {
  $args = array(
    'label' => __('Release Form Required','woocommerce'), // Text in the editor label
    'value' => get_post_meta(get_the_id(), 'release_req', true), // meta_value where the id serves as meta_key
    'id' => 'release_req', // required, it's the meta_key for storing the value (is checked or not)
    'name' => __('Release Required','woocommerce'),
    'cbvalue' => true, // "value" attribute for the checkbox
    'desc_tip' => true, // true or false, show description directly or as tooltip
    'description' => __('Tells if a release form is required for this product.','woocommerce') // provide something useful here
  );
  woocommerce_wp_checkbox( $args );
}

add_action( 'woocommerce_product_options_advanced', 'add_release_checkbox' );

function save_release_meta($post_id) {
    write_log($_POST); // for debugging purposes write the post to the debug.log file
    $release = isset($_POST['release_req']) ? $_POST['release_req'] : '0';
    $product = wc_get_product($post_id);
    $product->update_meta_data('release_req', $release);
    $product->save();
}
add_action('woocommerce_process_product_meta', 'save_release_meta');

I make this a text box it works, so the question is what am I doing wrong? It does not save even when I check the box or uncheck it.

Upvotes: 1

Views: 2125

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29624

The reason this doesn't work is because of the following that you use in your code

'name' => __('Release Required','woocommerce'),

name does not have to be translatable and is applied in the wrong way


Below a working answer. Where I have used woocommerce_admin_process_product_object to save instead of the outdated woocommerce_process_product_meta hook

function add_release_checkbox() {
    // Add checkbox
    $args = array(
        'label' => __( 'Release Form Required','woocommerce' ), // Text in the editor label
        'id' => 'release_req', // required, it's the meta_key for storing the value (is checked or not)
        'desc_tip' => true, // true or false, show description directly or as tooltip
        'description' => __('Tells if a release form is required for this product.','woocommerce') // provide something useful here
    );
    woocommerce_wp_checkbox( $args );
}

add_action( 'woocommerce_product_options_advanced', 'add_release_checkbox' );

// Save Field
function action_woocommerce_admin_process_product_object( $product ) {
    // Isset, yes or no
    $checkbox = isset( $_POST['release_req'] ) ? 'yes' : 'no';

    // Update meta
    $product->update_meta_data( 'release_req', $checkbox );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 ); 

Upvotes: 3

Related Questions