Woocommerce将Costum字段添加到订单meta和admin订单

时间:2019-02-25 15:39:16

标签: php wordpress woocommerce

使用以下代码,我在woocommerce产品上获得了自定义字段。但是...如何在订单,结帐和管理员订单(后端)上显示它?这是详细的屏幕截图

这是我对每种产品的woocommerce的自定义字段

My product custom fields

这是我的管理订单(后端)详细信息。我尝试显示我在每个产品发布中输入的corect元数据。

My backend

如何在我的管理订单中显示我在自定义字段中输入的值?我也希望有人能帮助我按顺序显示并显示结帐页面...或指导我正确的方向。为了使它按管理员顺序显示,我必须对其进行关联,我必须确保我按命令或结帐页面注册了meta。

// Display Fields

add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');

// Save Fields

add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');

function woocommerce_product_custom_fields()
    {
    global $woocommerce, $post;
    echo '<div class="product_custom_field">';

    // Custom Product Text Field

    woocommerce_wp_text_input(array(
        'id' => '_custom_product_text_field',
        'placeholder' => 'Custom Product Text Field',
        'label' => __('Custom Product Text Field', 'woocommerce') ,
        'desc_tip' => 'true'
    ));

    // Custom Product Number Field

    woocommerce_wp_text_input(array(
        'id' => '_custom_product_number_field',
        'placeholder' => 'Custom Product Number Field',
        'label' => __('Custom Product Number Field', 'woocommerce') ,
        'type' => 'number',
        'custom_attributes' => array(
            'step' => 'any',
            'min' => '0'
        )
    ));

    // Custom Product  Textarea

    woocommerce_wp_textarea_input(array(
        'id' => '_custom_product_textarea',
        'placeholder' => 'Custom Product Textarea',
        'label' => __('Custom Product Textarea', 'woocommerce')
    ));
    echo '</div>';
    }

function woocommerce_product_custom_fields_save($post_id)
    {

    // Custom Product Text Field

    $woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
    if (!empty($woocommerce_custom_product_text_field)) update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));

    // Custom Product Number Field

    $woocommerce_custom_product_number_field = $_POST['_custom_product_number_field'];
    if (!empty($woocommerce_custom_product_number_field)) update_post_meta($post_id, '_custom_product_number_field', esc_attr($woocommerce_custom_product_number_field));

    // Custom Product Textarea Field

    $woocommerce_custom_procut_textarea = $_POST['_custom_product_textarea'];
    if (!empty($woocommerce_custom_procut_textarea)) update_post_meta($post_id, '_custom_product_textarea', esc_html($woocommerce_custom_procut_textarea));
    }

// Display in order edit

add_action('woocommerce_admin_order_data_after_billing_address', 'display_verification_id_in_admin_order_meta', 10, 1);

function display_verification_id_in_admin_order_meta($order)
    {

    // compatibility with WC +3

    $order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id;
    $total_incart = number_format((float)$order->get_total() - $order->get_total_tax() - $order->get_total_shipping() - $order->get_shipping_tax() , wc_get_price_decimals() , '.', '');
    $post_meta_value = get_post_meta($post->ID, '_custom_product_text_field', true);
    echo '<p><strong>' . __('TEXTFIELD', 'woocommerce') . ':</strong> ' . get_post_meta($order_id, '_custom_product_text_field', true) . '</p>';
    echo '<p><strong>' . __('NUMBERFIELD', 'woocommerce') . ':</strong> ' . get_post_meta($order_id, '_custom_product_number_field', true) . '</p>';
    echo '<p><strong>' . __('TEXTAREAFIELD', 'woocommerce') . ':</strong> ' . get_post_meta($order_id, '_custom_product_textarea', true) . '</p>';
    echo '<p><strong>Total price items: </strong>' . $total_incart . '</p>';
    }

1 个答案:

答案 0 :(得分:2)

已经快一年了,但我认为它可以帮助某人...

本文确切地解释了所要提出的问题。

Add New Fields to the Order Details Metabox

我认为解决问题的文章中最相关的部分

 /**
 * Display custom fields in to Admin order details
 */
add_action( 'woocommerce_admin_order_data_after_order_details', 'custom_woocommerce_admin_order_data_after_order_details' );
function custom_woocommerce_admin_order_data_after_order_details( $order ){
?>
    <br class="clear" />
    <h4>My Custom Checkout Field Name <a href="#" class="edit_custom_field">Edit</a></h4>
    <?php 
        /*
         * get all the meta data values we need
         */ 
        $custom_field_value = get_post_meta( $order->id, 'my_custom_field_name', true );
    ?>
    <div class="custom_field">
        <p><strong>Custom Field Name:</strong> <?php echo wpautop( $custom_field_value ) ?></p>
    </div>

    <div class="edit_custom_field"> <!-- use same css class in h4 tag -->
    <?php
        woocommerce_wp_textarea_input( array(
            'id' => 'custom_field_name',
            'label' => 'Custom Field Name:',
            'value' => $custom_field_value,
            'wrapper_class' => 'form-field-wide'
        ) );
    ?>
    </div>
<?php
}

/**
 * Save the custom fields values
 */
add_action( 'woocommerce_process_shop_order_meta', 'custom_woocommerce_process_shop_order_meta' );
function custom_woocommerce_process_shop_order_meta( $order_id ){
    update_post_meta( $order_id, 'custom_field_name', wc_sanitize_textarea( $_POST[ 'custom_field_name' ] ) );
}

文章内有一个链接,其中他解释了如何在结帐页面上添加字段。

WooCommerce Checkout Fields