WooCommerce可变产品后台复选框元数据:勾选保存+仅管理员可见
Hey Brad, let's fix those two problems you're facing with your WooCommerce variable product checkboxes right away. Here's how to adjust your code to meet both requirements:
1. Only Save Checkbox Meta When Checked (Avoid Empty Values)
Your current code saves a space character when the checkbox is unchecked—let's update the save function to delete the meta key entirely when the box isn't checked, instead of storing an empty value. This keeps your post meta clean and avoids unnecessary data.
Replace your existing save_variation_settings_fields function with this:
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 ); function save_variation_settings_fields( $variation_id, $loop ) { // Handle bin location text input (keep existing logic, optional delete if empty) if( isset( $_POST['bin_location'][ $loop ] ) ) { update_post_meta( $variation_id, 'bin_location', esc_attr( $_POST['bin_location'][ $loop ] ) ); } else { delete_post_meta( $variation_id, 'bin_location' ); // Remove if input is empty } // Handle MCPS checkbox: save 'yes' only if checked, else delete meta if( isset( $_POST['licence_mcps'][ $loop ] ) ) { update_post_meta( $variation_id, 'licence_mcps', 'yes' ); } else { delete_post_meta( $variation_id, 'licence_mcps' ); } // Handle PPL checkbox: same logic as MCPS if( isset( $_POST['licence_ppl'][ $loop ] ) ) { update_post_meta( $variation_id, 'licence_ppl', 'yes' ); } else { delete_post_meta( $variation_id, 'licence_ppl' ); } }
2. Hide Meta Data Everywhere Except Order Admin Dashboard
To ensure these meta fields only appear in the order edit screen (and not on the frontend, order emails, or customer account pages), we'll add a filter to control meta display. Add this new function to your code:
add_filter( 'woocommerce_order_item_display_meta_filter', 'hide_order_meta_from_frontend', 10, 3 ); function hide_order_meta_from_frontend( $formatted_meta, $item, $order ) { // Define the meta keys we want to restrict to admin only $admin_only_meta = array( 'licence_mcps', 'licence_ppl', 'bin_location' ); foreach( $formatted_meta as $key => $meta ) { // Remove meta if we're not in the admin dashboard if( in_array( $meta->key, $admin_only_meta ) && !is_admin() ) { unset( $formatted_meta[ $key ] ); } } return $formatted_meta; }
Optional: Clean Up Admin Meta Labels
If you want the meta fields to look more user-friendly in the order admin (instead of raw key names), tweak your save_file_type_as_order_item_meta function to add custom labels:
add_action('woocommerce_checkout_create_order_line_item', 'save_file_type_as_order_item_meta', 20, 4); function save_file_type_as_order_item_meta($item, $cart_item_key, $values, $order) { $variation = $values['data']; if ( $licence_mcps = $variation->get_meta('licence_mcps') ) { $item->update_meta_data( __( 'MCPS Licence', 'woocommerce' ), $licence_mcps ); } if ( $bin_location = $variation->get_meta('bin_location') ) { $item->update_meta_data( __( 'Bin Location', 'woocommerce' ), $bin_location ); } if ( $licence_ppl = $variation->get_meta('licence_ppl') ) { $item->update_meta_data( __( 'PPL Licence', 'woocommerce' ), $licence_ppl ); } }
How It All Works
- Checkbox Saving: Now when a checkbox is unchecked, its corresponding meta key is removed from the variation's post meta—no empty or placeholder values left behind.
- Meta Visibility: The filter checks if we're outside the WordPress admin area; if so, it removes the specified meta fields from display. Customers won't see them anywhere, but you'll still have full access in the order edit screen.
内容的提问来源于stack exchange,提问作者Brad Holmes

