TEST '; e-6ren">
gpt4 book ai didi

php - 保存选择字段的(多个)元值

转载 作者:可可西里 更新时间:2023-10-31 23:47:00 25 4
gpt4 key购买 nike

我无法保存选择字段框中包含的元数据。举个例子(卡在 function.php 到单品设置页面):

(...)


<select style="width:100%" id="_receita" name="_receita[]" multiple="multiple" data-placeholder="<?php _e( 'Search for a product', 'woocommerce' ); ?>">
<?php
echo '<option value="TEST"> TEST </option>';
echo '<option value="TEST1"> TEST1 </option>';
echo '<option value="TEST2"> TEST2 </option>';
?>
</select>

// few others custom fields:
<?php $ingrediente_x_quantidade = get_post_meta( $post->ID, '_ingrediente_x_quantidade', true ); ?>
<input placeholder="<?php _e( 'Ingrediente', 'woocommerce' ); ?>" type="text" class="ingrediente" id ="_ingrediente" name="_ingrediente" value="<?php echo $ingrediente_x_quantidade[0]; ?>" style="width: 90%;" />
<input placeholder="<?php _e( 'qnt.', 'woocommerce' ); ?>" type="number" name="_quantidade" id="_quantidade" class = "quantidade" value="<?php echo $ingrediente_x_quantidade[1]; ?>" step="any" min="0" style="width: 25%;" />

(...)

为了保存数据,我在 function.php 中包含:

    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );


function woo_add_custom_general_fields_save( $post_id ){

// Custom Field
$ingrediente_x_quantidade = array( esc_attr( $_POST['_ingrediente'] ), esc_attr( $_POST['_quantidade'] ) );
update_post_meta( $post_id, '_ingrediente_x_quantidade', $ingrediente_x_quantidade );

// Select Field
foreach ($_POST["_receita"] as $receita) {
update_post_meta($post->ID, "_receita", $receita);
}

}

自定义字段(“_ingrediente_x_quantidade”)一切正常,但对于选择字段“_receita”,我得到一个空数组:

[_ingrediente_x_quantidade] => Array
(
[0] => a:2:{i:0;s:19:”Aveia, flocos, crua”;i:1;s:3:”113″;}
)

[_receita] => Array
(
[0] =>
)

我做错了什么?我对编码还很陌生!

最佳答案

我发现使用 foreach 存储数据时有一个问题,每条记录都有相同的键和帖子 ID...

尝试在基地使用这个:

update_post_meta($post->ID, "_receita", serialize($receita));

$receita = unserialize(get_post_meta($post->ID, "_receita", true));

对空数组使用检查也很好......

关于php - 保存选择字段的(多个)元值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31497660/

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