我可以使用woocommerce products custom fields
但是,我需要在我的管理区域中的多个复选框中显示多个值。我有以下代码:
<input type="checkbox" name="myoption[option_one]" value="1"<?php checked( isset( $options[\'option_one\'] ) ); ?> />
<input type="checkbox" name="myoption[option_two]" value="1"<?php checked( isset( $options[\'option_two\'] ) ); ?> />
现在,我如何才能将它们保存到一个元字段中?与下面的想法类似?
$product_field_type = $_POST[\'product_field_type\'];
update_post_meta( $post_id, \'_product_field_type_ids\', $product_field_type );
EDIT
好的,我已经能够在我的产品页面中定义一个自定义框。但我的复选框没有存储!我错过了什么?
/* Define the custom box */
add_action( \'add_meta_boxes\', \'wpse_61041_add_custom_box\' );
/* Do something with the data entered */
add_action( \'save_post\', \'wpse_61041_save_postdata\' );
/* Adds a box to the main column on the Post and Page edit screens */
function wpse_61041_add_custom_box() {
add_meta_box( \'wpse_61041_sectionid\', \'title_color\', \'wpse_61041_inner_custom_box\', \'product\', \'normal\', \'high\' );
}
/* Prints the box content */
function wpse_61041_inner_custom_box($post)
{
// Use nonce for verification
wp_nonce_field( \'wpse_61041_wpse_61041_field_nonce\', \'wpse_61041_noncename\' );
// Get saved value, if none exists, "default" is selected
$saved = get_post_meta( $post->ID, \'title_color\', true);
if( !$saved )
$saved = \'default\';
$fields = array(
\'red\' => __(\'Red\', \'wpse\'),
\'green\' => __(\'Green\', \'wpse\'),
\'blue\' => __(\'Blue\', \'wpse\'),
\'default\' => __(\'Default\', \'wpse\'),
);
foreach($fields as $key => $label)
{
printf(
\'<input type="checkbox" name="title_color" value="%1$s" id="title_color[%1$s]" %3$s />\'.
\'<label for="title_color[%1$s]"> %2$s \' .
\'</label><br>\',
esc_attr($key),
esc_html($label),
checked($saved, $key, false)
);
}
}
/* When the post is saved, saves our custom data */
function wpse_61041_save_postdata( $post_id )
{
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST[\'wpse_61041_noncename\'], \'wpse_61041_wpse_61041_field_nonce\' ) )
return;
if ( isset($_POST[\'title_color\']) && $_POST[\'title_color\'] != "" ){
update_post_meta( $post_id, \'title_color\', $_POST[\'title_color\'] );
}
}