1

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

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
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you! It mostly does, instead of a $ amount I am getting "Array" in the column but that still identifies them so I'm still better off than I was! – Laura Nixon Jun 27 '23 at 05:45
  • 1
    Array means that the data is not a string but has an array of values… To see all that values, you can replace the code *(for debug purpose)* `echo $store_credit;` with `echo '
    ' . print_r($store_credit, true) . '
    ';`. It will display the array data… Then copy and paste the array that you will get at the end of your question. Then notify me here.
    – LoicTheAztec Jun 27 '23 at 06:28
  • 1
    One thing - the data is overlapping the shipping column. https://snipboard.io/O7LYTD.jpg However the [amount] and [cart total] data is exactly what i would want to view. – Laura Nixon Jun 28 '23 at 23:12
  • @LauraNixon I have updated my answer code… It's normal that the **data is overlapping** as this is just **for testing** to get the complete array of key / value pairs, to see what key we need to get the correct display… With my update, you will get the formatted amount (with currency). – LoicTheAztec Jun 29 '23 at 00:10