gpt4 book ai didi

php - 在后端产品选项卡中检索自定义字段的标签名称

转载 作者:行者123 更新时间:2023-12-01 22:18:26 25 4
gpt4 key购买 nike

下面是带有自定义字段、自定义选项卡及其内容的 WooCommerce 自定义产品:

我正在对该选项卡的第一个文本字段进行采样。 目标是获取这些字段的“标签”属性。

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 的自定义产品:

WooCommerce Custom Tab with Custom Text Field

现在,使用 ACF,我使用了这段代码:

<?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);

enter image description here

有人说 WooCommerce 项目没有绑定(bind) ACF 对象?这就是我无法通过 ACF 访问 WooCommerce 对象的原因?你的想法。

谢谢!

最佳答案

UPDATE (A WORKIG SOLUTION TO SAVE AND RETRIEVE YOUR LABELS 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'] );
}

}

代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

提交后(已保存) 所有数据都设置在当前产品 ID 的 wp_postmeta 表中(甚至是标签名称),请参阅下面是你在这个数据库表中得到的(这里的产品 ID 是 99):

enter image description here

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 文件中使用这个函数:

<?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

此代码已经过测试并且可以工作...

关于php - 在后端产品选项卡中检索自定义字段的标签名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42116037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com