在你的問題中提供的代碼是不完整的,應該是這樣的:
// Enabling and Displaying Fields in backend
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields');
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(array(// Text Field type
'id' => '_conditions',
'label' => __('Conditions', 'woocommerce'),
'placeholder' => 'i.e: brand-new; refurbished; defected...',
'desc_tip' => 'true',
'description' => __('Enter the conditions of the products here.', 'woocommerce')
));
woocommerce_wp_text_input(array(// Text Field type
'id' => '_brands', // ===> NOT '_bands'
'label' => __('Brands', 'woocommerce'),
'placeholder' => 'i.e: Lacoste; Hugo Boss...etc',
'desc_tip' => 'true',
'description' => __('Enter names of the Brands of the products if any.', 'woocommerce')
));
echo '</div>'; // Closing </div> tag HERE
}
// Save Fields values to database when submitted (Backend)
add_action('woocommerce_process_product_meta', 'woo_save_custom_general_fields');
function woo_save_custom_general_fields($post_id){
// Saving "Conditions" field key/value
$conditions_field = $_POST['_conditions'];
if(!empty($conditions_field))
update_post_meta($post_id, '_conditions', esc_attr($conditions_field));
// Saving "Brands" field key/value
$brands_field = $_POST['_brands'];
if(!empty($brands_field))
update_post_meta($post_id, '_brands', esc_attr($brands_field));
}
現在,在您的產品頁面中顯示此元數據值時,您將使用get_post_meta()
函數進行掛鉤函數上。這裏下面你將看到woocommerce_single_product_summary
鉤所有的鉤狀模板及其優先級(顯示順序):
/**
* woocommerce_single_product_summary hook
*
* @hooked woocommerce_template_single_title - 5
* @hooked woocommerce_template_single_price - 10
* @hooked woocommerce_template_single_excerpt - 20
* @hooked woocommerce_template_single_add_to_cart - 30
* @hooked woocommerce_template_single_meta - 40
* @hooked woocommerce_template_single_sharing - 50
*/
的「庫存」或「SKU」數據由woocommerce_template_single_meta
其中擁有優先顯示現在你需要顯示你的關卡字段值。然後,您可以爲此選擇優先級。
這是在「SKU」要輸出的自定義字段中的值在產品頁面代碼:
add_action('woocommerce_single_product_summary', 'woo_display_custom_general_fields_values', 45);
function woo_display_custom_general_fields_values() {
global $product;
echo '<p class="custom-conditions">Conditions: ' . get_post_meta($product->id, '_conditions', true) . '</p>';
echo '<p class="custom-brands">Brands: ' . get_post_meta($product->id, '_brands', true) . '</p>';
}
代碼放在您的活動子主題的function.php文件(活動主題或任何插件文件)。
此代碼已經過測試並可正常工作。