0

I´m using Woocommerce with paid addon from automatic "product addon" which adds some meta values to the order item table

I want to show 2 or more specific meta key values in the admin order list.

  • 1 Funghi Topup: Salami Oil: chilli

I got the quantity and product name working with this code but not the order item meta, any ideas?

add_action('manage_shop_order_posts_custom_column', 'orders_list_preview_items', 20, 2 );
function orders_list_preview_items($column, $post_id) {
    
    global $the_order, $post;
    
    if ('order_status' === $column) {
        
        // Start list
        echo '<ul class="orders-list-items-preview">';
        
        // Loop through order items
        foreach($the_order->get_items() as $item) {
            
            $product = $item->get_product();
         
            
            $name    = $item->get_name();
            $qty     = $item->get_quantity();
        $meta    = $item->get_meta();
            
            echo "<li>
                
               <label>$qty</label> $name $meta
            </li>";
        }
        
        // End list
        echo '</ul>';
    }
    
    
}
Bhautik
  • 11,125
  • 3
  • 16
  • 38
Gery
  • 3
  • 3

2 Answers2

0

You can Loop through the item's metadata. and check for only the meta keys that you want to display. check the below code.

add_action('manage_shop_order_posts_custom_column', 'orders_list_preview_items', 20, 2);
function orders_list_preview_items($column, $post_id){
    global $the_order, $post;

    if ('order_status' === $column) {
        // Start list
        echo '<ul class="orders-list-items-preview">';

        // Loop through order items
        foreach ($the_order->get_items() as $item) {
            $product = $item->get_product();

            $name = $item->get_name();
            $qty = $item->get_quantity();
            $meta = $item->get_meta();

            echo "<li>
                <label>$qty</label> $name";

            // Loop through the item's meta data
            foreach ($meta as $meta_key => $meta_value) {
                // Display specific meta key values
                if (in_array($meta_key, ['Funghi Topup', 'Salami Oil', 'chilli'])) {
                    echo "<br>$meta_key: $meta_value";
                }
            }

            echo "</li>";
        }

        // End list
        echo '</ul>';
    }
}
Bhautik
  • 11,125
  • 3
  • 16
  • 38
  • thanks for your reply... I tried it but did not work, I think get_meta() only gets the order meta and not the order item meta. I tried get_item_meta() instead but it break the page. any ideas? – Gery Jul 19 '23 at 04:16
  • Did you print `$meta`? what is the output? – Bhautik Jul 19 '23 at 05:29
  • $meta should output all order item meta from the post id which is the order id, not sure how to get it but found this `$meta = wc_get_order_item_meta( $item_id, 'Topup', true );` – Gery Jul 19 '23 at 13:50
  • Does this `$meta = wc_get_order_item_meta( $item_id, 'Topup', true );` worked? – Bhautik Jul 19 '23 at 13:56
  • not yet, but I think `wc_get_order_item_meta( $item_id, 'Topup', true );`is the right one to use, `foreach ($meta as $meta_key => $meta_value) ` is wrong in this case I think... – Gery Jul 19 '23 at 16:33
0
'add_action('manage_shop_order_posts_custom_column', orders_list_preview_items', 20, 2);

function orders_list_preview_items($column, $post_id) { global $the_order, $post;

if ('order_status' === $column) {
    // Start list
    echo '<ul class="orders-list-items-preview">';

    // Loop through order items
    foreach ($the_order->get_items() as $item) {
        $product = $item->get_product();
        $name = $item->get_name();
        $qty = $item->get_quantity();
        $meta = $item->get_meta_data(); // Use get_meta_data() to retrieve item meta data

        echo "<li><label>$qty</label> $name ";

        // Display Topup and Oil meta values
        foreach ($meta as $meta_item) {
            if ($meta_item->key === 'Topup') {
                echo "<span style='color: green; line-height: 1.5;'>{$meta_item->value}</span> ";
            }
            if ($meta_item->key === 'Oil') {
                echo "<span style='color: brown; line-height: 1.5;'>{$meta_item->value}</span>";
            }
        }

        echo "</li>";
    }

    // End list
    echo '</ul>';
}

} code works ;-)

Gery
  • 3
  • 3
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Jul 30 '23 at 06:59