'How to associate a shipping method and products related to it in this order

Is it possible to divide the order by the method of shipping, to have information about which method of shipping has which products?

As in this photo

I can get all information about shipping methods in the order and product information from the order, but I can't associate a shipping method with a product that belongs to it in this order

DB - wp_woocommerce_order_itemmeta


To get information about the method of delivery and the product I write out as follows:

    foreach ($order->get_items('shipping') as $item_id => $item) {
        // Get the data in an unprotected array
        $item_data = $item_values->get_data();
        $email .= $item_id . ' <br>';
        $email .= $item_data['id'] . ' <br>';
        $email .= $item_data['order_id'] . ' <br>';
        $email .= $item_data['name'] . ' <br>';
        $email .= $item_data['method_title'] . ' <br>';
        $email .=  $item_data['method_id'] . ' - method_id <br>';
       ...
    }
    foreach ($order->get_items() as $item_key => $item_values) :
        $email .= $item_key . ' <br>';
        $item_data = $item_values->get_data();
        $product_id =  $item_data['product_id'];
        $email .=  $item_data['name'] . ' x';
        $email .=  $item_data['quantity'] . ' - ';
        $email .=  $item_data['subtotal'] . 'zł <br>';
        $product = $item_values->get_product(); // Get the WC_Product object
        $email .= $product->get_shipping_class() . '<br>';
        $email .=  $product->get_shipping_class_id();
        $email .= '<br><br><br><br>';
    endforeach;

And what do I get out of it:

  1. Info about shipping method

    42
    111
    inPost
    inPost
    flat_rate
    9
    14.99


    43
    111
    inPost
    inPost
    flat_rate
    9
    13.00


  2. Info about product and product shipping class

    39
    BAGGANÄS 22 new testowy produkt x12 - 1320zł
    craftowe
    19

    40
    BAGGANÄS x22 - 4378zł
    craftowe
    19

    41
    BAGGANÄS 22 jestes x11 - 1210zł
    jestes
    20

And then I had a problem separating products by shipping methods. Does anyone have any ideas? :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source