$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Sort by promo code used woocommerce admin panel|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Sort by promo code used woocommerce admin panel

matteradmin11PV0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I work on Woocommerce / Wordpress and I'm new, I want to custom my Woocommece orders list admin panel by adding the used coupons inside it, but I've got a little problem when i click on promo field for sort, it return an empty result.

In my function.php

function sv_wc_cogs_add_order_promo_column_header($columns)
{

    $new_columns = array();

    foreach ($columns as $column_name => $column_info) {

        $new_columns[$column_name] = $column_info;

        if ('order_total' === $column_name) {
            $new_columns['order_promo'] = __('Promo', 'my-textdomain');
        }
    }

    return $new_columns;
}

add_filter('manage_edit-shop_order_columns', 'sv_wc_cogs_add_order_promo_column_header', 20);



/**
 * Adds 'Profit' column content to 'Orders' page immediately after 'Total' column.
 *
 * @param string[] $column name of column being displayed
 */
add_action( 'manage_shop_order_posts_custom_column', 'sv_wc_cogs_add_order_promo_column_content',20 ,4 );
function sv_wc_cogs_add_order_promo_column_content( $column, $post ) {
    global $post;

    if ( 'order_promo' === $column ) {
        $order  = wc_get_order( $post->ID );
        $coupons = $order->get_used_coupons();

        foreach($coupons as $coupon){
            echo $coupon .'<br>';
        }
    }
}

add_filter( "manage_edit-shop_order_sortable_columns", 'custom_woo_admin_sort' );
function custom_woo_admin_sort( $columns )
{
    $custom = array(
        'order_promo'    => '_order_promo',
    );
    $sortable_columns[ '_order_promo' ] = 'order_promo';
    return wp_parse_args( $custom, $columns );
}


// Here is the problem cause the sort is not good ! It sort with strange value
add_action('pre_get_posts', 'custom_promocode_orderby');
function custom_promocode_orderby( $query ) {
    if ( !is_admin() ){ return; }

    $orderby = $query->get('orderby');
    if ('_order_promo' == $orderby){
        $meta_query = array(
            'relation' => 'OR',
            array(
                'key' => 'id',
                'compare' => 'NOT EXISTS',
            ),
            array(
                'key' => '_order_promo',
            ),
        );
      $query->set('order',    'ASC' );
      $query->set('orderby', $meta_query);
    }
}

Thank you very much for reading my question and for your futures answers. I can't test answers during weekend cause I don't have the project.

Sorry if my code is not a good practice ! I'm a noob in wordpress...

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I work on Woocommerce / Wordpress and I'm new, I want to custom my Woocommece orders list admin panel by adding the used coupons inside it, but I've got a little problem when i click on promo field for sort, it return an empty result.

In my function.php

function sv_wc_cogs_add_order_promo_column_header($columns)
{

    $new_columns = array();

    foreach ($columns as $column_name => $column_info) {

        $new_columns[$column_name] = $column_info;

        if ('order_total' === $column_name) {
            $new_columns['order_promo'] = __('Promo', 'my-textdomain');
        }
    }

    return $new_columns;
}

add_filter('manage_edit-shop_order_columns', 'sv_wc_cogs_add_order_promo_column_header', 20);



/**
 * Adds 'Profit' column content to 'Orders' page immediately after 'Total' column.
 *
 * @param string[] $column name of column being displayed
 */
add_action( 'manage_shop_order_posts_custom_column', 'sv_wc_cogs_add_order_promo_column_content',20 ,4 );
function sv_wc_cogs_add_order_promo_column_content( $column, $post ) {
    global $post;

    if ( 'order_promo' === $column ) {
        $order  = wc_get_order( $post->ID );
        $coupons = $order->get_used_coupons();

        foreach($coupons as $coupon){
            echo $coupon .'<br>';
        }
    }
}

add_filter( "manage_edit-shop_order_sortable_columns", 'custom_woo_admin_sort' );
function custom_woo_admin_sort( $columns )
{
    $custom = array(
        'order_promo'    => '_order_promo',
    );
    $sortable_columns[ '_order_promo' ] = 'order_promo';
    return wp_parse_args( $custom, $columns );
}


// Here is the problem cause the sort is not good ! It sort with strange value
add_action('pre_get_posts', 'custom_promocode_orderby');
function custom_promocode_orderby( $query ) {
    if ( !is_admin() ){ return; }

    $orderby = $query->get('orderby');
    if ('_order_promo' == $orderby){
        $meta_query = array(
            'relation' => 'OR',
            array(
                'key' => 'id',
                'compare' => 'NOT EXISTS',
            ),
            array(
                'key' => '_order_promo',
            ),
        );
      $query->set('order',    'ASC' );
      $query->set('orderby', $meta_query);
    }
}

Thank you very much for reading my question and for your futures answers. I can't test answers during weekend cause I don't have the project.

Sorry if my code is not a good practice ! I'm a noob in wordpress...

Share Improve this question edited Mar 8, 2019 at 9:11 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Mar 8, 2019 at 8:54 OliverOliver 152 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

After research I 've found a plugin at this adress:

https://github/bekarice/woocommerce-filter-orders

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far