Copy đoạn code sau và nhúng vào file functions.php để dùng.
<?php
// backend
add_filter( 'woocommerce_product_data_tabs', 'wpshare247_add_my_custom_product_data_tab' );
function wpshare247_add_my_custom_product_data_tab( $tabs ){
$tabs['wpshare247-custom-tab'] = array(
'label' => __('Wpshare247 TABS', 'woocommerce'),
'target' => 'wpshare247_custom_product_data',
'priority' => 25,
'class' => array('')
);
return $tabs;
}
add_action( 'woocommerce_product_data_panels', 'wpshare247_add_my_custom_product_data_fields' );
function wpshare247_add_my_custom_product_data_fields(){
?>
<div id="wpshare247_custom_product_data" class="panel woocommerce_options_panel">
<?php
//https://wpshare247.com/danh-sach-cac-field-cua-woocommerce-huu-dung-danh-cho-coder
//text input box
woocommerce_wp_text_input(
array(
'id' => 'wpshare247_wooc_text_bh',
'value' => get_post_meta( get_the_ID(), 'wpshare247_wooc_text_bh', true ),
'label' => __( 'Thời gian bảo hành', 'woocommerce' ),
'placeholder' => __( 'Vd: 12', 'woocommerce' ),
//'class' => 'wpshare247-c1 wpshare247-c2',
//'style' => 'color:red;',
//'wrapper_class' => 'wpshare247-wrapper-c1 wpshare247-wrapper-c2',
'type' => 'text',
'description' => __( 'Số tháng bảo hành', 'woocommerce' ),
'desc_tip' => true,
'data_type' => '' // '', price, decimal, stock, url
)
);
woocommerce_wp_text_input(
array(
'id' => 'wpshare247_wooc_text_db',
'value' => get_post_meta( get_the_ID(), 'wpshare247_wooc_text_db', true ),
'label' => __( 'Đã bán', 'woocommerce' ),
'placeholder' => __( 'Vd: 3000', 'woocommerce' ),
//'class' => 'wpshare247-c1 wpshare247-c2',
//'style' => 'color:red;',
//'wrapper_class' => 'wpshare247-wrapper-c1 wpshare247-wrapper-c2',
'type' => 'text',
'description' => __( 'Số sản phẩm đã bán', 'woocommerce' ),
'desc_tip' => true,
'data_type' => '' // '', price, decimal, stock, url
)
);
?>
</div>
<?php
}
add_action('woocommerce_process_product_meta', 'wpshare247_save_custom_tab_data');
function wpshare247_save_custom_tab_data( $post_id ){
$product = wc_get_product( $post_id );
$wpshare247_wooc_text_bh = isset( $_POST['wpshare247_wooc_text_bh'] ) ? $_POST['wpshare247_wooc_text_bh'] : '';
$wpshare247_wooc_text_db = isset( $_POST['wpshare247_wooc_text_db'] ) ? $_POST['wpshare247_wooc_text_db'] : '';
$product->update_meta_data('wpshare247_wooc_text_bh', sanitize_text_field($wpshare247_wooc_text_bh));
$product->update_meta_data('wpshare247_wooc_text_db', sanitize_text_field($wpshare247_wooc_text_db));
$product->save();
}
add_action('admin_head', 'wpshare247_woocommerce_product_data_admin_head');
function wpshare247_woocommerce_product_data_admin_head(){
?>
<style type="text/css">
#woocommerce-product-data ul.wc-tabs li.wpshare247-custom-tab_tab a::before{
content: "\f177";
}
</style>
<?php
}
//Frontend
add_action( 'woocommerce_single_product_summary', 'wpshare247_wooc_new_data', 11 );
function wpshare247_wooc_new_data(){
$bh = get_post_meta( get_the_ID(), 'wpshare247_wooc_text_bh', true );
if($bh){
?>
<div id="wpshare247_wooc_text_bh" style="color:red;">Bảo hành: <?php echo $bh;?> tháng</div>
<?php
}
$db = get_post_meta( get_the_ID(), 'wpshare247_wooc_text_db', true );
if($db){
?>
<div id="wpshare247_wooc_text_db" style="color:red;">Đã bán: <?php echo $db;?></div>
<?php
}
}