在后端产品标签中检索自定义字段的标签名称 [英] Retrieving the label name for a custom fields in backend product tabs
问题描述
下面是带有自定义字段,自定义标签和其内容的WooCommerce自定义产品
Below is the WooCommerce Custom Product with custom field, custom tab and it's content:
我正在此标签上采样第一个文本字段。 目标是获取这些字段的标签属性。
I'm sampling the first text field at this tab. Goal is to get the "label" property of these fields.
function launch_product_tab_content() {
global $post;
?><div id='launch_contents' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
woocommerce_wp_text_input( array(
'id' => '_text_announced',
'label' => __( 'Announced(Global)', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Year and Month it was announced global', 'woocommerce' ),
'type' => 'text',
) );
woocommerce_wp_text_input( array(
'id' => '_text_announced_ph',
'label' => __( 'Announced(Philippines)', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Year and Month it was announced global', 'woocommerce' ),
'type' => 'text',
) );
woocommerce_wp_text_input( array(
'id' => '_text_availability_ph',
'label' => __( 'Availability(Philippines)', 'woocommerce'),
'desc_tip' => 'true',
'description' => __( 'Schedule date of availability in the Philippines', 'woocommerce' ),
'type' => 'text',
) );
?></div>
</div><?php
}
add_action( 'woocommerce_product_data_panels', 'launch_product_tab_content' );
这是产品编辑器页面(Wordpress的自定义产品)上的样子:
This is what it looks like at the Product editor page, Custom Product at Wordpress:
现在,使用ACF,我使用了此代码:
Now, using ACF, I used this code:
<?php
$field_key = "_text_announced";
$post_id = $post->ID;
$field = get_field_object($field_key, $post_id);
echo $field['label'] . ': ' . $field['value'];
?>
还尝试了 echo var_dump($ field);
有人说WooCommerce项目没有绑定到ACF对象?这就是为什么我无法通过ACF访问WooCommerce对象的原因?您的想法。
Someone said that the WooCommerce project is not binded to ACF object? That's why I can't access the WooCommerce object via ACF? Your thoughts.
谢谢!
推荐答案
更新(保存和检索标签名称的可行解决方案)
我进行了一些更改在您的代码中添加带有标签名称的隐藏的插补字段。保存/提交数据时,它还将自动保存标签名称。
I Have make some changes In your code adding hidden imput fields with your label names. When saving/submitting the data, it will save also automatically the label names.
这里是完整的代码:
// ADDING A TAB TO WOOCOMMERCE PRODUCT DATA METABOX
add_filter( 'woocommerce_product_data_tabs', 'launch_product_tab_content_tab' , 99 , 1 );
function launch_product_tab_content_tab( $product_data_tabs ) {
$product_data_tabs['launch'] = array(
'label' => __( 'Launch', 'my_text_domain' ),
'target' => 'launch_contents',
);
return $product_data_tabs;
}
// ADDING A FIELDS INSIDE THE TAB IN WOOCOMMERCE PRODUCT DATA METABOX
add_action( 'woocommerce_product_data_panels', 'launch_product_tab_content' );
function launch_product_tab_content() {
global $woocommerce, $post;
// Setting here your labels
$label_text_announced = __( 'Announced(Global)', 'woocommerce' );
$label_text_announced_ph = __( 'Announced(Philippines)', 'woocommerce' );
$label_text_availability_ph = __( 'Availability(Philippines)', 'woocommerce' );
?>
<div id='launch_contents' class='panel woocommerce_options_panel'>
<div class='options_group'>
<?php
woocommerce_wp_text_input( array(
'id' => '_text_announced',
'label' => $label_text_announced,
'desc_tip' => 'true',
'description' => __( 'Year and Month it was announced global', 'woocommerce' ),
'type' => 'text',
) );
woocommerce_wp_text_input( array(
'id' => '_text_announced_ph',
'label' => $label_text_announced_ph,
'desc_tip' => 'true',
'description' => __( 'Year and Month it was announced global', 'woocommerce' ),
'type' => 'text',
) );
woocommerce_wp_text_input( array(
'id' => '_text_availability_ph',
'label' => $label_text_availability_ph,
'desc_tip' => 'true',
'description' => __( 'Schedule date of availability in the Philippines', 'woocommerce' ),
'type' => 'text',
) );
// Addind hidden imputs fields for your labels
echo '<input type="hidden" id="text_announced_label" name="text_announced_label" value="'.$label_text_announced.'" />
<input type="hidden" id="text_announced_ph_label" name="text_announced_ph_label" value="'.$label_text_announced_ph.'" />
<input type="hidden" id="text_availability_ph_label" name="text_availability_ph_label" value="'.$label_text_availability_ph.'" />';
?>
</div>
</div>
<?php
}
// SAVING THE FIELDS DATA from THE TAB IN WOOCOMMERCE PRODUCT DATA METABOX
add_action( 'woocommerce_process_product_meta', 'save_launch_product_tab_content' );
function save_launch_product_tab_content( $post_id ){
// Saving the data with the hidden data labels names
if(isset($_POST['_text_announced'])){
update_post_meta( $post_id, '_text_announced', $_POST['_text_announced'] );
update_post_meta( $post_id, '_text_announced_label', $_POST['text_announced_label'] );
}
if(isset($_POST['_text_announced_ph'])){
update_post_meta( $post_id, '_text_announced_ph', $_POST['_text_announced_ph'] );
update_post_meta( $post_id, '_text_announced_ph_label', $_POST['text_announced_ph_label'] );
}
if(isset($_POST['_text_availability_ph'])){
update_post_meta( $post_id, '_text_availability_ph', $_POST['_text_availability_ph'] );
update_post_meta( $post_id, '_text_availability_ph_label', $_POST['text_availability_ph_label'] );
}
}
代码生效您的活动子主题(或主题)或任何插件文件中的.php文件。
一旦提交(保存)所有数据均在 wp_postmeta
表中设置,以获取当前产品ID(甚至标签名称),请参见下面的数据库表(此处产品的ID为99):
因此,现在您可以获取标签名称和相应的数据值……
So now you can get your label name and the corresponding data value…
现在有一个函数可以自动执行该过程,并在数组中设置这些值:
function get_label_and_value($product_id, $meta_key){
// As the meta_key of the label have the same slug + '_label' we get it here
$key_label = $meta_key . '_label';
// Getting the values
$meta_value = get_post_meta($product_id, $meta_key, true);
$label_name = get_post_meta($product_id, $key_label, true);
// Setting this data in an array:
$result = array('label' => $label_name, 'value' => $meta_value);
// Returning the data array
return $result;
}
代码包含在您活动的子主题的function.php文件中(
现在我们可以在任何PHP文件中使用此功能:
Now we can use this function in any PHP file:
<?php
// The product ID
$product_id = $product_id;
// The field key
$field_key = "_text_announced";
// Using our function
$field1 = get_label_and_value($product_id, $field_key);
// Displaying the data (just as you expected to do)
echo $field1['label'] . ': ' . $field1['value'];
?>
您将获得:
Announced(Global): April 2016
因此这里不需要ACF
此代码已经过测试,可以正常工作...
This code is tested and works...
这篇关于在后端产品标签中检索自定义字段的标签名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!