Php 在编辑订单页面自定义元框中获取自定义字段值
我试图显示我在帖子管理区域或单个订单页面的自定义字段占位符上放置的任何值 我已经将Php 在编辑订单页面自定义元框中获取自定义字段值,php,wordpress,woocommerce,custom-fields,orders,Php,Wordpress,Woocommerce,Custom Fields,Orders,我试图显示我在帖子管理区域或单个订单页面的自定义字段占位符上放置的任何值 我已经将get\u post\u meta(get\u id(),'total usd',true)放在引号value=“”中,所以它不应该是空的,但当我单击更新时它仍然显示为空 我的代码好像出了什么问题?我感谢你在这方面的帮助 这是我的密码: <?php function cpmb_add_meta_box() { add_meta_box( 'woocommerce-order-my-
get\u post\u meta(get\u id(),'total usd',true)
放在引号value=“”
中,所以它不应该是空的,但当我单击更新时它仍然显示为空
我的代码好像出了什么问题?我感谢你在这方面的帮助
这是我的密码:
<?php
function cpmb_add_meta_box() {
add_meta_box(
'woocommerce-order-my-custom',
'USD Currency display',
'cpmb_display_meta_box',
'shop_order',
'normal',
'core'
);
}
add_action( 'add_meta_boxes', 'cpmb_add_meta_box');
function cpmb_display_meta_box( $post ) {
wp_nonce_field( plugin_basename( __POST__ ), 'cpmb-nonce-field' );
$html = '<label id="total-usd" for="total-usd">';
$html .= '<strong>Total USD Currency</strong>';
$html .= '</label><br />';
$html .= '<input type="text" id="total-usd" name="Total USD Currency" value="' . get_post_meta( get_the_id(), 'total-usd', true ) . '" placeholder="Enter Total here" />';
echo $html;
}
function cpmb_save_meta_box_data( $post_id ) {
if ( cpmb_user_can_save( $post_id, 'cpmb-nonce-field' ) ){
if ( isset( $_POST['total-usd'] ) && 0 < count( strlen( trim($_POST['total-usd'] )))) {
$total_usd = stripslashes( strip_tags($_POST['total-usd']));
update_post_meta( $post_id, 'total-usd', $total_usd );
}
}
}
add_action( 'save_post', 'cpmb_save_meta_box_data' );
function cpmb_user_can_save( $post_id, $nonce ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce($_POST[ $nonce ], plugin_basename( __POST__ ) ) );
return ! ( $is_autosave || $is_revision ) && $is_valid_nonce;
}
?>
您的主要问题在于您的
文本字段属性name=“Total USD Currency”
。应改为name=“total usd”
。此外,htmlid
属性应该是唯一的(不能在和html标记中设置相同的id)
我已更改了您的代码:
// Adding the metabox (on the right side)
add_action( 'add_meta_boxes', 'cpmb_add_meta_box');
function cpmb_add_meta_box() {
add_meta_box(
'woocommerce-order-my-custom',
__('USD Currency display'),
'cpmb_display_meta_box',
'shop_order',
'side',
'core'
);
}
// The metabox content
function cpmb_display_meta_box( $post ) {
// Get
$total_usd = get_post_meta( $post->ID, 'total-usd', true );
echo '<input type="hidden" name="cpmb_total_usd_nonce" value="' . wp_create_nonce() . '">
<label class="total-usd" for="total-usd">
<strong>Total USD Currency</strong></label><br />
<input type="text" id="total-usd" name="total-usd" value="' . $total_usd . '" placeholder="'. __("Enter Total here").'" />';
}
// Save/Update the meta data
add_action( 'save_post', 'cpmb_save_meta_box_data' );
function cpmb_save_meta_box_data( $post_id ) {
// Only for shop order
if ( 'shop_order' != $_POST[ 'post_type' ] )
return $post_id;
## Security verifications. ##
// Check if our nonce is set (and our cutom field)
if ( ! isset( $_POST[ 'cpmb_total_usd_nonce' ] ) && isset( $_POST['total-usd'] ) )
return $post_id;
$nonce = $_POST[ 'cpmb_total_usd_nonce' ];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) )
return $post_id;
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
if ( ! current_user_can( 'edit_shop_order', $post_id ) && ! current_user_can( 'edit_shop_orders', $post_id ) )
return $post_id;
## SETTING AND UPDATING DATA (SECURITY PASSED) ##
update_post_meta( $post_id, 'total-usd', sanitize_text_field( $_POST[ 'total-usd' ] ) );
}
//添加元盒(在右侧)
添加动作('add_meta_box'、'cpmb_add_meta_box');
函数cpmb_add_meta_box(){
添加元框(
“我的习惯”,
__(‘美元货币显示’),
“cpmb\u显示元框”,
"店单",,
"一边",,
“核心”
);
}
//代谢箱内容
功能cpmb_显示_元_框($post){
//得到
$total\u usd=get\u post\u meta($post->ID,'total usd',true);
回声'
美元总货币
';
}
//保存/更新元数据
添加操作('save_post','cpmb_save_meta_box_data');
功能cpmb_保存元框_数据($post_id){
//仅适用于商店订单
如果('shop\u order'!=$\u POST['POST\u type'])
返回$post_id;
##安全验证##
//检查当前值是否已设置(以及cutom字段)
如果(!isset($_POST['cpmb_total\u usd\u nonce')和&isset($_POST['total-usd']))
返回$post_id;
$nonce=$_POST['cpmb_total_usd_nonce'];
//验证nonce是否有效。
如果(!wp\u verify\u nonce($nonce))
返回$post_id;
//检查是否为自动保存
if(已定义('DOING_AUTOSAVE')&&DOING_AUTOSAVE)
返回$post_id;
//检查用户权限(针对“店铺经理”和“管理员”用户角色)
如果(!当前用户可以('edit_shop_order',$post_id)&&!当前用户可以('edit_shop_order',$post_id))
返回$post_id;
##设置和更新数据(已传递安全性)##
更新发布元($发布id,'总美元',清理文本字段($发布['总美元]));
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中
这段代码在WooCommerce 3+版本上进行了测试,可以正常工作
您将获得以下信息:
我非常感谢,谢谢你,很抱歉有后续问题。我如何回应显示的显示已经被0.75这样的数字所除?如果不是100,它将显示133.33?@Floranteferer请。。。我想是的,它回答了…你说的是什么计算,因为它不在你的问题中?所以也许你应该澄清你关于“计算”的问题…