Sort by promo code used woocommerce admin panel

admin2025-06-02  1

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748829440a314070.html

最新回复(0)