在WooCommerce中我试图在前端显示一些自定义数据:
第一个代码*( https://stackoverflow.com/a/39010336/8523129
第二段代码:
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
echo '<div class="product_custom_field">';
// WooCommerce custom dropdown Select
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __( 'WooCommerce Custom Select Field', 'woocommerce' ),
'options' => array(
'one' => __( 'First Dropdown', 'woocommerce' ),
'two' => __( 'Second Dropdown', 'woocommerce' ),
'three' => __( 'Third Dropdown', 'woocommerce' )
)
)
);
//WooCommerce Custom Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_checkbox',
'wrapper_class' => 'show_if_simple',
'label' => __('WooCommerce Custom Checkbox Field', 'woocommerce' )
)
);
// WooCommerce Custom field Type
?>
<p class="form-field custom_field_type">
<label for="custom_field_type"><?php echo __( 'WooCommerce Custom Field Type', 'woocommerce' ); ?></label>
<span class="wrap">
<?php $custom_product_field_type = get_post_meta( $post->ID, '_custom_field_type', true ); ?>
<input placeholder="<?php _e( 'Field One', 'woocommerce' ); ?>" class="" type="number" name="_field_one" value="<?php echo $custom_product_field_type[0]; ?>" step="any" min="0" style="width: 80px;" />
<input placeholder="<?php _e( 'Field Two', 'woocommerce' ); ?>" type="number" name="_field_two" value="<?php echo $custom_product_field_type[1]; ?>" step="any" min="0" style="width: 80px;" />
</span>
</p>
<?php
?>
<?php
echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id)
{
// WooCommerce custom dropdown Select
$woocommerce_custom_product_select = $_POST['_select'];
if (!empty($woocommerce_custom_product_select))
update_post_meta($post_id, '_select', esc_attr($woocommerce_custom_product_select));
//WooCommerce Custom Checkbox
$woocommerce_custom_product_checkbox = isset($_POST['_checkbox']) ? 'yes' : 'no';
update_post_meta($post_id, '_checkbox', $woocommerce_custom_product_checkbox);
// WooCommerce Custom field Type
$custom_product_field_type = array(esc_attr($_POST['_field_one']), esc_attr($_POST['_field_two']));
update_post_meta($post_id, '_custom_field_type', $custom_product_field_type);
}
// Custom Product Variation Settings
add_filter( 'woocommerce_available_variation', 'custom_load_variation_settings_products_fields' );
function custom_load_variation_settings_products_fields( $variations ) {
// duplicate the line for each field
$variations['_select'] = get_post_meta( $variations[ 'variation_id' ], '_select', true );
return $variations;
}
我在显示页面上没有得到任何结果。
如何实现这一目标。
答案 0 :(得分:1)
根据您的评论,要在单个产品页面的自定义产品标签中获取并显示产品自定义字段中的数据,您将以这种方式使用 get_post_meta()
功能:
// Add product custom product tab
add_filter( 'woocommerce_product_tabs', 'custom_product_tab' );
function custom_product_tab( $tabs ) {
$tabs['custom_tab'] = array(
'title' => __( 'Custom Data', 'woocommerce' ),
'priority' => 50,
'callback' => 'custom_data_product_tab_content'
);
return $tabs;
}
// The custom product tab content
function custom_data_product_tab_content(){
global $post;
// 1. Get the data
$select = get_post_meta( $post->ID, '_select', true );
$checkbox = get_post_meta( $post->ID, '_checkbox', true );
$cf_type = get_post_meta( $post->ID, '_custom_field_type', true );
// 2. Display the data
$output = '<div class="custom-data">';
if( ! empty( $select ) )
$output .= '<p>'. __('Select field: ').'</span style="color:#96588a;">'.$select.'</span></p>';
if( ! empty( $checkbox ) )
$output .= '<p>'. __('Checkbox field: ').'</span style="color:#96588a;">'.$checkbox.'</span></p>';
if( ! empty( $cf_type[0] ) )
$output .= '<p>'. __('Number field 1: ').'</span style="color:#96588a;">'.$cf_type[0].'</span></p>';
if( ! empty( $cf_type[1] ) )
$output .= '<p>'. __('Number field 2: ').'</span style="color:#96588a;">'.$cf_type[1].'</span></p>';
echo $output.'</div>';
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
经过测试和工作。您将获得类似的内容(并且您的数据将被提取如果不为空):