按定制订单分类Meta-WooCommerce订单管理页面

时间:2015-08-07 作者:Tuesdave

我正在尝试通过“订单”管理屏幕上的\\u payment\\u方法对我的Woocommerce订单进行排序(edit.php?post_type=shop_order).

到目前为止,我可以按“Pay\\u by\\u phone”排序,但它可以排序everytime 页面加载,这当然不理想。

function filter_orders_pay_by_phone($query) {
    global $pagenow;
    $qv = &$query->query_vars;

    if ( $pagenow == \'edit.php\' &&
        isset($qv[\'post_type\']) && $qv[\'post_type\'] == \'shop_order\' ) {
        $query->set(\'meta_key\', \'_payment_method\');
        $query->set(\'meta_value\', \'Pay by phone\');
    }

    return $query;
}
add_filter(\'pre_get_posts\', \'filter_orders_pay_by_phone\');
我需要在这个名为“电话订单”的管理页面中插入一个按钮或另一个排序选项(当前的选项是All、Trash、Processing等)单击后会导致页面刷新并添加此过滤器,以便仅显示这些订单。

我不知道有什么钩子可以用来生成这个功能。

1 个回复
SO网友:Leon Francis Shelhamer

在这里,我创建了一个函数来获取某个meta\\u键的所有可能的meta\\u值。然后,我钩住restrict\\u manage\\u posts,将meta\\u值的新select字段添加到过滤器表单中。最后,我钩住了parse\\u查询,将新过滤器添加到post查询中。

// Global to be used to stop filter from running on get_posts() in get_meta_values()
$GLOBALS[\'run_meta_filter_on_query\'] = true;

// function to grab all possible meta values of the chosen meta key in this case \'_payment_method\'
function get_meta_values( $meta_key,  $post_type = \'post\' ) {

    $GLOBALS[\'run_meta_filter_on_query\'] = false;
    $posts = get_posts(
        array(
            \'post_type\' => $post_type,
            \'meta_key\' => $meta_key,
            \'posts_per_page\' => -1,
        )
    );
    $GLOBALS[\'run_meta_filter_on_query\'] = true;

    $meta_values = array();
    foreach( $posts as $post ) {
        $meta_values[] = get_post_meta( $post->ID, $meta_key, true );
    }

    return $meta_values;

}

//Hook the filter options form
add_action(\'restrict_manage_posts\',\'add_meta_value_to_posts\');

function add_meta_value_to_posts(){

    // only add filter to shop_order
    global $post_type;
    if( $post_type == \'shop_order\' ) {

        // function to grab all possible meta values of the chosen meta key in this case \'_payment_method\'
        $meta_values = get_meta_values(\'_payment_method\', \'shop_order\');

        // Generate select field from meta values
        echo \'<select name="_payment_method" id="_payment_method">\';

            $all_selected = sanitize_text_field($_GET[\'_payment_method\']) == \'all\' ? \' selected\' : \'\';
            echo \'<option value="all"\'.$all_selected.\'>All</option>\';

            foreach ( $meta_values as $meta_value ) {
                $selected = sanitize_text_field($_GET[\'_payment_method\']) == $meta_value ? \' selected\' : \'\';
                echo \'<option value="\'.$meta_value.\'"\'.$selected.\'>\'.$meta_value.\'</option>\';
            }

        echo \'</select>\';

    }

}

// Hook parse_query to add new filter parameters
add_action(\'parse_query\',\'filter_posts_per_meta_value\');

function filter_posts_per_meta_value( $query ) {

    global $pagenow, $post_type;
    // Only add parmeeters if on shop_order and if all is not selected
    if( $pagenow == \'edit.php\' && $post_type == \'shop_order\' && !empty($_GET[\'_payment_method\']) && $_GET[\'_payment_method\'] != \'all\' && $GLOBALS[\'run_meta_filter_on_query\'] ) {

        $query->query_vars[\'meta_query\'][] = array(
            \'key\' => \'_payment_method\',
            \'value\' => $_GET[\'_payment_method\'],
            \'compare\' => \'=\',
        );

    }

}

结束

相关推荐

Archive not sorting correctly

我有一个名为“cif事件”的自定义帖子类型,我想按wpcf开始时间中的值进行排序。我在函数中添加了以下内容。php:add_filter(\'pre_get_posts\', \'set_order_for_archives\'); function set_order_for_archives($q) { if ($q->is_post_type_archive(\'cif-groups\')) { $q->set(\'orderby\', \'