PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 8, 2022

[FIXED] Add a column with coupons used on admin Orders list in Woocommerce

 January 08, 2022     backend, orders, php, woocommerce, wordpress     No comments   

Issue

I am trying to display the coupon(s) used directly on the order admin screen (where all orders are listed in WooCommerce) but for some reason it's giving me a fatal error because of the break.

Here is the code ->

add_filter( 'manage_shop_order_posts_columns', 'woo_customer_order_coupon_column_for_orders', 99 );
function woo_customer_order_coupon_column_for_orders( $columns ) {
$columns['order_coupon'] = __('Coupon', 'woocommerce');
return $columns;
}

add_action( 'manage_shop_order_posts_custom_column' , 'woo_display_customer_order_coupon_in_column_for_orders', 10, 2 );
function woo_display_customer_order_coupon_in_column_for_orders( $column_name, $order_id, $order ) {
    switch ( $column_name ) {
        case 'order_coupon':
        if( $order->get_used_coupons() ) {
        $coupons_count = count( $order->get_used_coupons() );
        echo '<h4>' . __('Coupons Used') . ' (' . $coupons_count . ')</h4>';
        echo '<p><strong>' . __('Coupons Used') . ':</strong> ';
        $i = 1;

        foreach( $order->get_used_coupons() as $coupon) {
            echo $coupon;
            if( $i < $coupons_count )
                echo ', ';
            $i++;
        }
        echo '</p>';
    } } break; }



add_filter( "manage_edit-shop_order_sortable_columns", 'woo_ordercoupon_sortable' );
function woo_ordercoupon_sortable( $columns ) {
$custom = array (
'order_coupon'    => 'Coupon', );
return wp_parse_args( $custom, $columns ); }

Any ideas on how to fix this would be very much appreciated.


Solution

There is some errors and mistakes in your code… Try the following instead:

add_filter( 'manage_edit-shop_order_columns', 'woo_customer_order_coupon_column_for_orders' );
function woo_customer_order_coupon_column_for_orders( $columns ) {
    $new_columns = array();

    foreach ( $columns as $column_key => $column_label ) {
        if ( 'order_total' === $column_key ) {
            $new_columns['order_coupons'] = __('Coupons', 'woocommerce');
        }

        $new_columns[$column_key] = $column_label;
    }
    return $new_columns;
}

add_action( 'manage_shop_order_posts_custom_column' , 'woo_display_customer_order_coupon_in_column_for_orders' );
function woo_display_customer_order_coupon_in_column_for_orders( $column ) {
    global $the_order, $post;
    if( $column  == 'order_coupons' ) {
        if( $coupons = $the_order->get_used_coupons() ) {
            echo implode(', ', $coupons) . ' ('.count($coupons).')';
        } else {
            echo '<small><em>'. __('No coupons') . '</em></small>';
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

(This can't be a sortable column)



Answered By - LoicTheAztec
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing