wordpress - 我想从 WooCommerce 管理员编辑页面编辑结帐字段
问题描述
我在 WooCommerce 结帐页面上添加了一些自定义字段。我已经保存了这些字段的数据并将其显示在管理订单页面上。就像图片 1 一样。一切都好。
现在我想编辑该字段的数据/文本/值。就像图片2一样。就像管理订单页面上的计费和运输一样......
我该如何解决?
function display_admin_order_meta ( $order ) {
$strt= $order->get_meta('_checkout_custom_name', true );
if( ! empty( $strt) ){
$label = __( 'Phone' );
if( is_admin() ){
echo '<p><strong>' . $label . ' : </strong> ' . $strt. '</p>';
}
else {
echo '<table class="woocommerce-table"><tbody><tr>
<th>' . $label . ' : </th><td>' . $strt. '</td>
</tr></tbody></table>';
}
}
$strt= $order->get_meta('_checkout_others_address', true );
if( ! empty( $strt) ){
$label = __( 'Address' );
if( is_admin() ){ // Admin
echo '<p><strong>' . $label . ' : </strong> ' . $strt. '</p>';
}
else {
echo '<table class="woocommerce-table"><tbody><tr>
<th>' . $label . ' : </th><td>' . $strt. '</td>
</tr></tbody></table>';
}
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_admin_order_meta', 20, 1);
这是我创建的结帐字段数据。我想像上面的图片一样编辑这些。
解决方案
这是一种方式(如果需要,可以自定义)
使用 woocommerce_admin_billing_fields 过滤器添加自定义计费表单数据输入
add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');
function add_woocommerce_admin_billing_fields($billing_fields) {
$billing_fields['_checkout_custom_name'] = array( 'label' => __('Name', 'woocommerce') );
$billing_fields['_checkout_others_address'] = array( 'label' => __('Address', 'woocommerce') );
return $billing_fields;
}
然后像这样保存自定义字段
function cloudways_save_extra_details( $post_id, $post ){
update_post_meta( $post_id, '_checkout_custom_name', $_POST[ '_checkout_custom_name' ] ) );
update_post_meta( $post_id, '_checkout_custom_address', $_POST[ '_checkout_custom_address' ] ) );
}
add_action( 'woocommerce_process_shop_order_meta', 'cloudways_save_extra_details', 45, 2 );
推荐阅读
- asp.net-core-identity - 未收到 ASP.net 核心标识 Yahoo 电子邮件声明
- java - 打印带有重复字符的字符串
- git - git 推送到 gitlab 非常慢
- sass - 我想将 scss 文件转换为 NEXT.js 中的每个 css 文件
- usb - 如果将多扇区引导加载程序写入其第一个扇区,则 USB 驱动器无法启动
- javascript - Firestore 获取子集合数据 onDelete
- perl - 在perl中使用命名管道在2个进程之间进行通信?
- javascript - 将受污染的画布下载为 PNG
- python - 正则表达式也不返回分隔模式
- python - Tkinter Image 仅显示最后一张