Php 在订单编辑页面上添加自定义元框,并将其显示在客户订单页面上

Php 在订单编辑页面上添加自定义元框,并将其显示在客户订单页面上,php,wordpress,woocommerce,meta-boxes,orders,Php,Wordpress,Woocommerce,Meta Boxes,Orders,在WooCommerce中,希望在WooCommerce管理订单页面上添加自定义元框 在这个框中,我只想在保存到该顺序的文本字段中输入一个跟踪编号 然后在customer view order页面上,我想显示一个按钮,打开一个包含跟踪信息的模式。模态将只拉入一个iframe,其URL在末尾有跟踪号 我使用的快递公司有一个跟踪网站,所以现在我只想使用在管理订单页面上输入的跟踪号码在模式中显示iframe 如果这不合理,请告诉我 如何保存和使用它 到目前为止,我有: // Add meta box

在WooCommerce中,希望在WooCommerce管理订单页面上添加自定义元框

在这个框中,我只想在保存到该顺序的文本字段中输入一个跟踪编号

然后在customer view order页面上,我想显示一个按钮,打开一个包含跟踪信息的模式。模态将只拉入一个iframe,其URL在末尾有跟踪号

我使用的快递公司有一个跟踪网站,所以现在我只想使用在管理订单页面上输入的跟踪号码在模式中显示iframe

如果这不合理,请告诉我

如何保存和使用它

到目前为止,我有:

// Add meta box
function tcg_tracking_box() {
    add_meta_box(
        'tcg-tracking-modal',
        'The Courier Guy Tracking',
        'tcg_meta_box_callback',
        'shop_order',
        'side',
        'high'
    );
}
add_action( 'add_meta_boxes', 'tcg_tracking_box' );

// Callback
function tcg_meta_box_callback( $post )
{
    $values = get_post_custom( $post->ID );
    $text = isset( $values['tcg_tracking_box'] ) ? esc_attr( $values['tcg_tracking_box'][0] ) : '';
    echo '<input type="text" name="tcg_tracking_box" id="tcg_tracking_box" value="' . $text . '" />';
}

// Saving
add_action( 'save_post', 'tcg_tracking_box_save' );
function tcg_tracking_box_save( $post_id )
{

}
//添加元框
功能tcg_跟踪_盒(){
添加元框(
“tcg跟踪模式”,
“快递员跟踪”,
“tcg_meta_box_回调”,
"店单",,
"一边",,
“高”
);
}
添加操作(“添加元框”、“tcg跟踪框”);
//回拨
函数tcg\u meta\u box\u回调($post)
{
$values=get\u post\u custom($post->ID);
$text=isset($values['tcg\u tracking\u box'])?esc\u attr($values['tcg\u tracking\u box'][0]):“”;
回声';
}
//拯救
添加操作('save_post'、'tcg_tracking_box_save');
功能tcg_跟踪_盒_保存($post_id)
{
}

您可以通过多种方式完成。我已更正了您的代码,并在末尾添加了一个自定义挂钩函数,该函数将在我的帐户订单视图页面中显示此自定义字段值:

// Add meta box
add_action( 'add_meta_boxes', 'tcg_tracking_box' );
function tcg_tracking_box() {
    add_meta_box(
        'tcg-tracking-modal',
        'The Courier Guy Tracking',
        'tcg_meta_box_callback',
        'shop_order',
        'side',
        'core'
    );
}

// Callback
function tcg_meta_box_callback( $post )
{
    $value = get_post_meta( $post->ID, '_tracking_box', true );
    $text = ! empty( $value ) ? esc_attr( $value ) : '';
    echo '<input type="text" name="tracking_box" id="tcg_tracking_box" value="' . $text . '" />';
    echo '<input type="hidden" name="tracking_box_nonce" value="' . wp_create_nonce() . '">';
}

// Saving
add_action( 'save_post', 'tcg_save_meta_box_data' );
function tcg_save_meta_box_data( $post_id ) {

    // Only for shop order
    if ( 'shop_order' != $_POST[ 'post_type' ] )
        return $post_id;

    // Check if our nonce is set (and our cutom field)
    if ( ! isset( $_POST[ 'tracking_box_nonce' ] ) && isset( $_POST['tracking_box'] ) )
        return $post_id;

    $nonce = $_POST[ 'tracking_box_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;

    // Saving the data
    update_post_meta( $post_id, '_tracking_box', sanitize_text_field( $_POST[ 'tracking_box' ] ) );
}

// Display To My Account view Order
add_action( 'woocommerce_order_details_after_order_table', 'tcg_display_tracking_box_in_order_view', 10, 1 );
function tcg_display_tracking_box_in_order_view( $order )
{
    $tracking_box = get_post_meta( $order->get_id(), '_tracking_box', true );
    // Output Tracking box
    if( ! empty( $tracking_box ) && is_account_page() )
        echo '<p>Tracking box: '. $tracking_box .'</p>';
}
//添加元框
添加操作(“添加元框”、“tcg跟踪框”);
功能tcg_跟踪_盒(){
添加元框(
“tcg跟踪模式”,
“快递员跟踪”,
“tcg_meta_box_回调”,
"店单",,
"一边",,
“核心”
);
}
//回拨
函数tcg\u meta\u box\u回调($post)
{
$value=get_post_meta($post->ID,'u tracking_box',true);
$text=!empty($value)?esc_attr($value):“”;
回声';
回声';
}
//拯救
添加操作('save_post','tcg_save_meta_box_data');
功能tcg\ U保存\元\框\数据($post\ id){
//仅适用于商店订单
如果('shop\u order'!=$\u POST['POST\u type'])
返回$post_id;
//检查当前值是否已设置(以及cutom字段)
如果(!isset($\u POST['tracking\u box\u nonce'])和&isset($\u POST['tracking\u box']))
返回$post_id;
$nonce=$\u POST['tracking\u box\u 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,''跟踪框',清理文本框($发布['跟踪框]));
}
//显示到我的帐户视图顺序
添加操作('WOOMerce\u order\u details\u在\u order\u表之后,'tcg\u在\u order\u视图中显示\u tracking\u box\u',10,1);
功能tcg_显示_跟踪_框_顺序_视图($order)
{
$tracking\u box=get\u post\u meta($order->get\u id(),'u tracking\u box',true);
//输出跟踪盒
if(!empty($tracking_box)&&is_account_page())
回显“跟踪框:”.$Tracking_box.

”; }
代码位于活动子主题(或主题)的function.php文件或任何插件文件中


这段代码在woocommerce 3+版上进行了测试,可以正常工作

Wow!非常感谢你!它就像一个符咒!!!!我在插件中添加了它,效果非常好。谢谢