Search code examples
phpwordpresswoocommerceorderscoupon

Add a 2nd column with custom metadata in WooCommerce Admin Orders


I have a custom column in WooCommerce showing coupons used (see screenshot) added via a code snippet.

I'd like to add another that shows store credit used in $ amount on each order,

I'm using Advanced coupons, and they've told me that orders with store credit would have acfw_store_credits_order_paid meta on them.

Any idea if i can copy the existing code snippet and add this metadata into it?

// 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);
        }
    }
}

Update:

When I use echo '<pre>' . print_r($store_credit, true) . '</pre>'; in your code I get the following array keys:

enter image description here


Solution

  • Updated

    I have added a 2nd column for your "store credit" custom metadata.

    Try the following code replacement:

    // 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');
                $reordered_columns['store_credit'] = __( 'Store credit', '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);
            }
        }
    
        if ( 'store_credit' === $column ) {
            // Get the data array for store credit on the current order
            $scredit = $the_order->get_meta('acfw_store_credits_order_paid');
            
            if ( ! empty($scredit) ) {
                // Format the credit amount with the correct currency
                echo wc_price( $scredit['raw_amount'], array('currency' => $scredit['currency']) );
            }
        }
    }
    
    

    It should work.


    From your custom field array, you can use the following:

    • $scredit['amount'] to get the credit amount
    • $scredit['raw_amount'] to get the credit raw amount
    • $scredit['cart_total'] to get the cart total
    • $scredit['currency'] to get the currency