【发布时间】:2014-01-11 13:43:56
【问题描述】:
我一直在使用 Wordpress 和 Woocommerce 向我的管理产品页面添加自定义元字段。我一切正常,只是当我保存产品时,它没有显示我在元框中输入的信息。
我检查了数据库,数据确实保存了,我看到我在值中输入的数据,元字段名称在元键中。所以这让我相信一切正常,除了在产品保存后在管理产品页面上显示值。
我认为问题出在这两个函数之一,但不确定,它可能在 save 函数中,尽管它调用了 update_post_meta。
这是我认为我的问题所在:
function variable_fields( $loop, $variation_data ) {
?>
<tr>
<td>
<div>
<label><?php _e( 'My Custom Field', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[<?php echo $loop; ?>]" value="<?php echo $variation_data['_my_custom_field'][0]; ?>"/>
</div>
</td>
</tr>
<?php
}
function variable_fields_js() {
?>
<tr>
<td>
<div>
<label><?php _e( 'My Custom Field', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[' + loop + ']" />
</div>
</td>
</tr>
<?php
}
这是保存数据的功能,以防万一问题出在这里?
function variable_fields_process( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
$variable_custom_field = $_POST['my_custom_field'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $variable_custom_field[$i] ) ) {
update_post_meta( $variation_id, '_my_custom_field', stripslashes( $variable_custom_field[$i] ) );
}
endfor;
endif;
}
查看代码并没有发现任何问题。一切正常,除了当我保存产品时,输入的数据不显示(但它在数据库中)所以我认为它可能与保存时发生冲突,在字段中显示保存的值。
感谢您的帮助
更新: 具体来说,我认为是这条线让我感到困惑:
value="<?php echo $variation_data['_my_custom_field'][0]; ?>"
【问题讨论】:
标签: php wordpress woocommerce