woocommerce offtopic – Display custom product option in admin order details

Question

I’m trying to display the custom product option in admin order panel.

I was able to pass all the data until the order is completed, but in the admin panel the data is returned is empty array.

I share the code

add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product_fitting_color', 10, 4 );
function save_order_item_product_fitting_color( $item, $cart_item_key, $cart_item, $order ) {
    
        //$item->update_meta_data( 'orderSpec', $cart_item['orderSpec'] );
         // The WC_Product instance Object
         $product = $item->get_product();
    
         // Get value
         $cart_item = $product->get_meta( 'orderSpec' );
         
         // NOT empty
         if ( ! empty ( $cart_item ) ) {
             $item->update_meta_data( 'orderSpec', $cart_item );
         }

    
}

// Add header
function action_woocommerce_admin_order_item_headers( $order ) {
    // Set the column name
    $column_name = __( 'Specifiche', 'woocommerce' );
    
    // Display the column name
    echo '<th  class="my-class">' . $column_name . '</th>';
}
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 1 );



//Add content
function action_woocommerce_admin_order_item_values( $product, $item, $item_id ) {
    // Only for "line_item" items type, to avoid errors
    if ( ! $item->is_type('line_item') ) return;

    // Get value
    $cart_item = $item->get_meta( 'orderSpec' );
    
    

    // NOT empty
    if ( ! empty ( $cart_item ) ) {
        echo '<td>';
         foreach($cart_item as $key => $value){ 
            
        echo '<strong>'.$key.'</strong> : '.$value;
    
    } 
    echo '</td>';
    } else {
        echo '<td>Meta not found!</td>';        
    }
}
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );

Thanks for your help

0
Simone Franceschetti 2 months 2022-06-10T07:30:25-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse