【问题标题】:Display custom checkout field value on admin order detail section in Woocommerce在 Woocommerce 的管理订单详细信息部分显示自定义结帐字段值
【发布时间】:2019-02-05 12:05:24
【问题描述】:

您好,我正在尝试在管理员订单详细信息页面中显示自定义结帐字段。我的自定义字段是 Delivery Option,它允许用户从复选框中选择一个值。我按照关于此的类似主题使用下面的代码,但我的代码似乎有问题。

add_action( 'woocommerce_review_order_after_shipping', 'checkout_shipping_additional_field', 20 );
function checkout_shipping_additional_field()
{
    $domain  = 'wocommerce';
    $default = 'option 1';

    echo '<tr class="additional-shipping-fields"><th>' . __('Delivery Time', $domain) . '</th><td>';

    // Add a custom checkbox field
    woocommerce_form_field( 'custom_radio_field', array(
        'type' => 'select',
        'class' => array( 'form-row-wide' ),
        'options' => array(
            'option 1' => __('10:04 : 13:04 ', $domain),

        ),
        'default' => $default,
    ), $default );

    echo '</td></tr>';
}

//update order meta
add_action('woocommerce_checkout_update_order_meta', 'gon_update_order_meta_business_address');

function gon_update_order_meta_business_address( $order_id ) {
    if ($_POST['custom_radio_field']) update_post_meta( $order_id, 'Business Address?', 
    esc_attr($_POST['custom_radio_field']));
}

// Display field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta( $order ){
    $delivery_time = get_post_meta( $order->get_id(), 'Delivery Time', true );
    if( ! empty( $delivery_time ) )
        echo '<p><strong>'.__('Delivery Time', 'woocommerce').': </strong> ' . $delivery_time . '</p>';
}

【问题讨论】:

    标签: wordpress woocommerce field checkout hook-woocommerce


    【解决方案1】:

    有一些错误,所以我重新审视了你的代码。我还更换了一些挂钩。请尝试以下操作:

    // HERE set your the options array for your select field.
    function delivery_time_options(){
        $domain = 'woocommerce';
        return array(
            '1' => __('10:04 : 13:04 ', $domain),
            '2' => __('14:04 : 16:04 ', $domain), // <== Added for testing
        );
    }
    
    // Display a custom select field after shipping total line
    add_action( 'woocommerce_review_order_after_shipping', 'checkout_shipping_additional_field', 20 );
    function checkout_shipping_additional_field(){
        $domain = 'woocommerce';
    
        echo '<tr class="additional-shipping-fields"><th>' . __('Delivery Time', $domain) . '</th><td>';
    
        // Add a custom select field
        woocommerce_form_field( 'delivery_time', array(
            'type' => 'select',
            'class' => array( 'form-row-wide' ),
            'options' => delivery_time_options(),
        ), '' );
    
        echo '</td></tr>';
    }
    
    // Save custom field as order meta data
    add_action('woocommerce_checkout_create_order', 'save_custom_field_order_meta', 22, 2 );
    function save_custom_field_order_meta( $order, $data ) {
        if ( isset($_POST['delivery_time']) ) {
            $options    = delivery_time_options(); // Get select options array
            $option_key = esc_attr($_POST['delivery_time']); // The selected key
    
            $order->update_meta_data( '_delivery_time', $options[$option_key] ); // Save
        }
    }
    
    // Display a custom field value on the admin order edit page
    add_action( 'woocommerce_admin_order_data_after_shipping_address', 'display_custom_meta_data_in_backend_orders', 10, 1 );
    function display_custom_meta_data_in_backend_orders( $order ){
        $domain = 'woocommerce';
    
        $delivery_time = $order->get_meta('_delivery_time');
        if( ! empty( $delivery_time ) )
            echo '<p><strong>'.__('Delivery Time', $domain).': </strong> ' . $delivery_time . '</p>';
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 您的回答很棒而且很有效,非常感谢@loictheaztec。我将其扩展为 DRY 方法,以防需要多个字段,而无需为每个字段重写此函数。
    【解决方案2】:

    基于@LoicTheAztec 的回答,如果您想要多个字段而不为每个字段重写函数 (DRY),您可以使用此类(通过将其添加到您的functions.php ):

    /**
     * Add a custom field to the woocommerce checkout page
     * https://stackoverflow.com/q/52098807/
     */
    class WOO_Add_Checkout_Field
    {
        public function __construct($options)
        {
    
            $this->field_name = $options['field_name'];
            $this->label = $options['label'];
            $this->placeholder = $options['placeholder'];
            $this->required = $options['required'];
    
            if ($this->field_name && $this->label && $this->placeholder) {
                add_action('woocommerce_after_order_notes', [$this, 'customise_checkout_field']);
                add_action('woocommerce_checkout_update_order_meta', [$this, 'custom_checkout_field_update_order_meta'], 10, 1);
                add_action('woocommerce_admin_order_data_after_billing_address', [$this, 'display_custom_field_on_order_edit_pages'], 10, 1);
            } else {
                die("Error in WOO_Add_Checkout_Field \$options: \n\n" . var_dump($options));
            }
        }
        public function customise_checkout_field($checkout)
        {
            echo '<div id="customise_checkout_field">';
            // echo '<h2>' . __('Heading') . '</h2>';
            woocommerce_form_field($this->field_name, array(
                'type' => 'text',
                'class' => array(
                    'my-field-class form-row-wide'
                ),
                'label' => $this->label,
                'placeholder' => $this->placeholder,
                'required' => $this->required,
            ), $checkout->get_value($this->field_name));
            echo '</div>';
        }
    
        public function custom_checkout_field_update_order_meta($order_id)
        {
            if (!empty($_POST[$this->field_name]))
                update_post_meta($order_id, $this->field_name, $_POST[$this->field_name]);
            else
                update_post_meta($order_id, $this->field_name, 0);
        }
    
    
        public function display_custom_field_on_order_edit_pages($order)
        {
            $field = $order->get_meta($this->field_name);
            if (!empty($field)) {
                echo '<p><strong style="display:block" title="' . $this->placeholder . '">' . $this->label . ': </strong><span>';
                echo $field;
                echo '</span></p>';
            }
        }
    }
    

    并根据需要多次使用它:

    $my_custom_field_1 = new WOO_Add_Checkout_Field([
        'field_name' => 'my_custom_field_1',
        'label' => __('My First Field'),
        'placeholder' => __('Please write something in field 1...'),
        'required' => false,
    ]);
    
    $my_custom_field_2 = new WOO_Add_Checkout_Field([
        'field_name' => 'my_custom_field_2',
        'label' => __('My Second Field'),
        'placeholder' => __('Please write something in field 2...'),
        'required' => false,
    ]);
    
    $my_custom_field_3 = new WOO_Add_Checkout_Field([
        'field_name' => 'my_custom_field_3',
        'label' => __('My Third Field'),
        'placeholder' => __('Please write something in field 3...'),
        'required' => false,
    ]);
    
    // and so on...
    

    请注意:

    • 只有在未发送空的情况下,自定义字段才会显示在管理区域中
    • 您可以根据自己的喜好自定义此代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-30
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      相关资源
      最近更新 更多