在WooCommerce管理订单列表自定义列上显示订单商品元数据

时间:2020-07-22 12:09:10

标签: php wordpress woocommerce metadata orders

我一直在尝试自行搜索此信息,但失败了。

我要做的只是将数据从wp_woocommerce_order_items表传递到我的自定义插件,以便能够在我的自定义列中显示优惠券代码,并且不知道如何使它工作。我已经在下面尝试过代码,但是没有用。

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

如果我尝试传递$ order_id,它是如何在我发现的一些教程中制成的,我得到的列类型为Int(13),而不是每个订单的实际价值。

1 个答案:

答案 0 :(得分:0)

要获取自定义订单项元数据,请使用以下命令(您将'some_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);
    }
}

应该可以

相关: