1

I've been trying to search for this info on my own, but I failed.

All I am trying to do is to pass data from wp_woocommerce_order_items table to my custom plugin, to be able to display coupon code in my custom columns, and can't figure out how to make it works. I've tried code below, but it doesn't work.

add_action( 'manage_shop_order_posts_custom_column', 'getSellersAndCouponValues', 2, 2);
function getSellersAndCouponValues( $column, $order ) { // ...my code }

And if I try to pass $order_id, how it was made in some tutorial I found, I am getting column type of Int(13), instead of real value for every single order.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • You can not get these callback functions called and pass just any arbitrary parameters you like. The callback function for this action gets _one_ parameter passed, and it is an array. So go and check what that contains. – CBroe Jul 22 '20 at 12:14
  • ok, thanks. so my callback function takes one arg - $column. I have printed out every single object, and its not very usefull to me. ` // etc ` – ThereIsNoSpoon Jul 22 '20 at 12:25

1 Answers1

4

To get custom order item meta data use the following (where you will replace 'some_meta_key' by the desired order item meta key):

add_action( 'manage_shop_order_posts_custom_column', 'get_sellers_and_coupon_values' );
function get_sellers_and_coupon_values( $column ) {
    global $post, $the_order;
    
    $order_id = $the_order->get_id();
    
    // Replace 'my_custom_column_key' with your desired column key
    if ( 'my_custom_column_key' === $column ) {
        $display = array();
        
        // Loop through order items
        foreach ( $the_order->get_items() as $item_id => $item ) {
            // $item_id is the current Item ID

            // Get the WC_Product Object
            $product = $item->get_product();
            
            // Get custom order item meta data | Replace 'some_meta_key' by the desired meta key
            $meta_value = $item->get_meta('some_meta_key');
            
            $display[$item_id] = $meta_value;
        }
        // Testing output
        echo implode(' | ', $meta_value);
    }
}

It should work

Related:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399