管理员通过自定义字段搜索Woocommerce订单

时间:2016-06-21 10:48:54

标签: php wordpress woocommerce

我已将自定义字段添加到我的结帐中,并且我想让它可搜索,以便使用它搜索订单。 该领域是手机,到目前为止的代码是:

function custom_admin_billing_fields( $fields ) {
    global $post;
    $order = $post->ID;
    $fields['phone']['show'] = false;
    $fields['mobile'] = array(
        'label' => __( 'Mobile', 'bootstrap' ),
        'value'=>get_post_meta( $order, '_billing_mobile', true ),
        'show'  => true
    );
    return $fields;
}
add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields',10,1 );

function custom_admin_shipping_fields( $fields ) {
    global $post;
    $order = $post->ID;
    $fields['phone']['show'] = false;
    $fields['mobile'] = array(
        'label' => __( 'Mobile', 'bootstrap' ),
        'value'=>get_post_meta( $order, '_shipping_mobile', true ),
        'show'  => true
    );

    return $fields;
}
add_filter( 'woocommerce_admin_shipping_fields', 'custom_admin_shipping_fields', 11, 1);

function woocommerce_shop_order_search_order_total( $search_fields ) {
    $search_fields[] = '_billing_mobile';
    $search_fields[] = '_shipping_mobile';

    return $search_fields;
}
add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_order_total' );

搜索不适用于手机,但是它适用于默认字段

前端代码(结账)

function custom_woocommerce_shipping_fields($fields) {
    $fields['shipping_phone']   = array(
        'label'          => __('Phone', 'woocommerce'),
        'placeholder'    => __('Phone', 'woocommerce'),
        'required'       => false,
        'class'          => array('form-row-last'),
    );

    $fields['shipping_mobile']  = array(
        'label'          => __('Mobile Phone', 'bootstrap'),
        'placeholder'    => __('Mobile Phone', 'bootstrap'),
        'required'       => true,
        'clear'          => true,
        'class'          => array('form-row'),
    );
    return $fields;
}
add_filter( 'woocommerce_shipping_fields', 'custom_woocommerce_shipping_fields');

function custom_woocommerce_billing_fields( $fields ) {
    $fields['billing_phone']['required']    = false;
    $fields['billing_mobile']   = array(
        'label'          => __('Mobile Phone', 'bootstrap'),
        'placeholder'    => __('Mobile Phone', 'bootstrap'),
        'required'       => true,
        'clear'          => true,
        'class'          => array('form-row-last'),
    );
    return $fields;
}
add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' );

1 个答案:

答案 0 :(得分:0)

我们有相同的需求通过“跟踪代码”查找订单(来自AST插件)。

这是在搜索中添加自定义列(以插入到 functions.php 中)的代码:

function woocommerce_shop_order_search_order_tracking_code($search_fields) { 
    $search_fields[] = '_woocommerce-advanced-shipment-tracking'; 
    return $search_fields;
}
add_filter('woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_order_tracking_code');

结果:

woo custom column in orders list search

希望有帮助。