【发布时间】:2018-04-16 23:46:18
【问题描述】:
我需要在 metabox 和前端 woocommerce 上显示单选按钮和多个复选框。我能够获取文本区域和其他字段,但无法在单个产品页面上获取多个复选框和单选按钮数据,无法填充、获取和检查每种类型的数据值。
add_filter( 'woocommerce_product_data_tabs', 'product_data_tab', 99 , 1 );
function product_data_tab( $product_data_tabs ) {
$product_data_tabs['shipping-costs'] = array(
'label' => __( 'Extra Product Info', 'my_theme_domain' ), // translatable
'target' => 'shipping_costs_product_data', // translatable
);
return $product_data_tabs;
}
add_action( 'woocommerce_product_data_panels', 'product_data_fields' );
function product_data_fields() {
global $post;
$post_id = $post->ID;
echo '<div id="shipping_costs_product_data" class="panel woocommerce_options_panel">'
$input_checkbox = get_post_meta( $post_id, '_input_checkbox', true );
if( empty( $input_checkbox ) ) $input_checkbox = ''; // set default value
$input_radio = get_post_meta( $post_id, '_input_radio', true );
if( empty( $input_radio ) ) $input_radio = ''; // set default value
// Checkbox field
woocommerce_wp_checkbox( array(
'id' => '_input_checkbox',
'wrapper_class' => 'show_if_simple',
'label' => __( 'Input checkbox Label', 'my_theme_domain' ),
'description' => __( 'Input checkbox Description', 'my_theme_domain' ),
// 'desc_tip' => __( 'Input checkbox Description tip', 'my_theme_domain' ),
// 'name' => 'input_checkbox',
//'cbvalue' => 'yes', // selected if same than 'value' (default value),
'options' => array(
'value1' => 'Option 1',
'value2' => 'Option 2',
'value3' => 'Option 3',
),
'value' => $input_checkbox, // <== POPULATING
) );
// Radio Buttons field
woocommerce_wp_radio( array(
'id' => '_input_radio',
'wrapper_class' => 'show_if_simple',
'label' => __('Delivery Period ', 'my_theme_domain'),
'description' => __( 'Delivery Period Description', 'my_theme_domain' ),
// 'desc_tip' => __( 'Input Radio Description tip', 'my_theme_domain' )
// 'name' => 'input_radio',
'options' => array(
'less than 5 days' => 'less than 5 days',
'10 days' => '10 days',
'15 days' => '15 days',
'30 days' => '30 days',
),
'value' => $input_radio, // <== POPULATING
) );
}
保存自定义产品选项卡元框的自定义字段数据:
add_action( 'woocommerce_process_product_meta', 'shipping_costs_process_product_meta_fields_save' );
function shipping_costs_process_product_meta_fields_save( $post_id ){
// save the checkbox field data
$wc_checkbox = isset( $_POST['_input_checkbox'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_input_checkbox', $wc_checkbox );
// save the radio button field data
$wc_radio = isset( $_POST['_input_radio'] ) ? $_POST['_input_radio'] : '';
update_post_meta( $post_id, '_input_radio', $wc_radio );
}
// Add product custom "custom" tab
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tab' );
function woo_custom_product_tab( $tabs ) {
$tabs['cust_tab'] = array(
'title' => __( 'Extra Product Info', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_custom_data_product_tab_content'
);
return $tabs;
}
// The Shipping tab content
function woo_custom_data_product_tab_content() {
global $post;
//Get the data ////get_post_meta( $post->ID
$select2 = get_post_meta( $post->ID, '_input_checkbox', true );
$select3 = get_post_meta( $post->ID, '_input_radio', true );
$output = '<div class="custom-data">';
if( ! empty( $select2 ) )
$output .= '<p>'. __('input_checkbox: ').'<span style="color:#96588a;">'.$select2.'</span></p>';
if( ! empty( $select3 ) )
$output .= '<p>'. __('Delivery Period: ').'<span style="color:#96588a;">'.$select3.'</span></p>';
echo $output.'</div>'
【问题讨论】:
-
@LoicTheAztec 添加了所需的代码。请检查
-
对于简单的变量都
-
@LoicTheAztec 错误地回滚了。 :(
-
@LoicTheAztec 我想我现在没有错过。
-
本题与woocommerce-rest-api标签无关……
标签: php wordpress woocommerce product hook-woocommerce