'Woocommerce get order products and assign to product vendor

I work with woocommerce multi vendor (MCFM plugin) and I try to get the sum of product prices for each vendor from the order.

As an example I have order with 3 produtcs in it, product_id => total_price:

1340 => 10$
1345 => 20$
1350 => 40$

Two products belongs to vendor id 20, one product belongs to vendor id 30. So product_id => vendor_id would looks like this:

1340 => 20;
1345 => 20;
1350 => 30;

I'm trying to assing products id to vendor In this step i got stuck

20 => 1340, 1345
30 => 1350

And then count the price of items, hope for that result vendor_id => total_price:

20 => 30$;
30 => 40$;

My code:

 $vendor_items_map = array();

        $order = wc_get_order( 18154 ); //Get specific order information

        foreach ( $order->get_items() as $item ) { //Get each product in order
            $product_ids = $item['product_id']; //Get each product ID
            $vendors[] = wcfm_get_vendor_id_by_post($item['product_id']); //Get each product vendor ID, var_dump($vendors) result string(2) "20" string(2) "20" string(2) "30"
            if($vendors){
            foreach( $vendors as $vendor) { //try to map each vendor id to product ID
                $vendor_items_map[$vendor] = $product_ids;
            }
        }

            foreach($vendor_items_map as $key => $product_ids){  
                $splited_items[$key] = array(
                    $product_ids,
                );
            }

        }

        print_r($splited_items);

My reslut:

Array
(
    [20] => Array
        (
            [0] => 1340 
        )

    [30] => Array
        (
            [0] => 1340 
        )

)

I expect to get:

Array
(
    [20] => Array
        (
            [0] => 1340
            [1] => 1345 
        )

    [30] => Array
        (
            [0] => 1350
        )

)

After I get what i expect I will try to count the total price of items and insted of product id i will set that price



Sources

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

Source: Stack Overflow

Solution Source