在订购编辑页面时添加一个自定义元框并将其显示在客户订单页面上

问题描述:

在WooCommerce中,想要在WooCommerce管理订单页面上添加自定义元框。在订购编辑页面时添加一个自定义元框并将其显示在客户订单页面上

在此框中,我只想在保存为该顺序的文本字段中输入一个跟踪号码。

然后在客户查看订单页面上,我想显示一个按钮,打开一个带有跟踪信息的模式。该模式只需将一个带有跟踪编号的URL的iframe拖入结尾即可。

我正在使用的快递公司有一个跟踪网站,所以现在我只是要使用在管理订单页面上输入的跟踪号码显示模式中的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) 
{ 

} 

您可以以多种方式做到这一点。我已经纠正了你的代码,增加了在最后一个自定义挂钩函数将在我的帐户订单查看页面显示该自定义字段值:

// 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>'; 
} 

代码放在您的活动子主题的function.php文件(或主题)还是在任何插件文件中。

此代码对woocommerce版本测试3+和工作

+1

哇!非常感谢你!它的作用像一个魅力!我将它添加到插件中,并且完美地工作。谢谢! – user2583295