Search code examples
phpwordpresswoocommerceorderscoupon

Display used coupons on WooCommerce admin orders list


Below code for a print coupon on order page

add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
function custom_shop_order_column($columns)
{
    $reordered_columns = array();

    
    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  'order_status' ){
            
            $reordered_columns['Coupons'] = __( 'Coupons','theme_domain');
        }
    }
    return $reordered_columns;
}

How I can print the current value of coupon for each user order??


Solution

  • Use the following to display used coupon codes in admin order list custom column:

    // Additional custom column
    add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
    function custom_shop_order_column( $columns ) {
        $reordered_columns = array();
    
        foreach( $columns as $key => $column){
            $reordered_columns[$key] = $column;
            if( $key ==  'order_status' ){
                $reordered_columns['coupons'] = __( 'Coupons', 'woocommerce');
            }
        }
        return $reordered_columns;
    }
    
    // Custom column content
    add_action( 'manage_shop_order_posts_custom_column', 'custom_shop_order_column_used_coupons' );
    function custom_shop_order_column_used_coupons( $column ) {
        global $post, $the_order;
    
        if ( ! is_a( $the_order, 'WC_Order' ) ) {
            $the_order = wc_get_order( $post->ID );
        }
    
        if ( 'coupons' === $column ) {
            $coupon_codes = $the_order->get_coupon_codes();
            
            if ( ! empty($coupon_codes) ) {
                echo implode(', ', $coupon_codes);
            }
        }
    }
    

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

    Related: Get coupon data from WooCommerce orders